Last active
January 18, 2018 13:55
-
-
Save vouill/ba32dd88739acd35a96929d578fc1066 to your computer and use it in GitHub Desktop.
token usage
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) => { | |
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