Last active
January 18, 2018 13:55
-
-
Save vouill/6c8a8408efb1f5e1d0088e842d91c4f9 to your computer and use it in GitHub Desktop.
Actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const actions = { | |
[AUTH_REQUEST]: ({commit, dispatch}, user) => { | |
return new Promise((resolve, reject) => { // The Promise used for router redirect in login | |
commit(AUTH_REQUEST) | |
axios({url: 'auth', data: user, method: 'POST' }) | |
.then(resp => { | |
const token = resp.data.token | |
localStorage.setItem('user-token', token) // store the token in localstorage | |
commit(AUTH_SUCCESS, token) | |
// you have your token, now log in your user :) | |
dispatch(USER_REQUEST) | |
resolve(resp) | |
}) | |
.catch(err => { | |
commit(AUTH_ERROR, err) | |
localStorage.removeItem('user-token') // if the request fails, remove any possible user token if possible | |
reject(err) | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment