Instantly share code, notes, and snippets.

Embed
What would you like to do?
Actions
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