Instantly share code, notes, and snippets.

Embed
What would you like to do?
token usage
const actions = {
[AUTH_REQUEST]: ({commit, dispatch}, user) => {
return new Promise((resolve, reject) => {
commit(AUTH_REQUEST)
axios({url: 'auth', data: user, method: 'POST' })
.then(resp => {
const token = resp.data.token
localStorage.setItem('user-token', token)
// Add the following line:
axios.defaults.headers.common['Authorization'] = token
commit(AUTH_SUCCESS, resp)
dispatch(USER_REQUEST)
resolve(resp)
})
.catch(err => {
commit(AUTH_ERROR, err)
localStorage.removeItem('user-token')
reject(err)
})
})
},
[AUTH_LOGOUT]: ({commit, dispatch}) => {
return new Promise((resolve, reject) => {
commit(AUTH_LOGOUT)
localStorage.removeItem('user-token')
// remove the axios default header
delete axios.defaults.headers.common['Authorization']
resolve()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment