Skip to content

Instantly share code, notes, and snippets.

@vouill
Last active January 18, 2018 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vouill/6c8a8408efb1f5e1d0088e842d91c4f9 to your computer and use it in GitHub Desktop.
Save vouill/6c8a8408efb1f5e1d0088e842d91c4f9 to your computer and use it in GitHub Desktop.
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