Skip to content

Commit

Permalink
Implementação da expiração
Browse files Browse the repository at this point in the history
  • Loading branch information
quaresmateo authored and lubien committed May 18, 2019
1 parent 7c20d78 commit 6941add
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
18 changes: 17 additions & 1 deletion plugins/axios.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
export default function({ $axios, store }) {
import { Toast } from 'buefy/dist/components/toast'
export default function({ $axios, store, redirect }) {
$axios.setToken(store.state.auth.token, 'Bearer')
$axios.onError(error => {
const code = parseInt(error.response && error.response.status)
if (
code === 400 &&
error.response.data.code === process.env.errors.INVALID_TOKEN
) {
Toast.open({
type: 'is-danger',
message: 'Login expirado',
duration: 5000
})
store.dispatch('auth/logout')
redirect('/login')
}
})
}
4 changes: 2 additions & 2 deletions store/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ export const state = () => ({

export const mutations = {
setUser(state, user) {
persist('user', user)
state.user = user
},

setToken(state, token) {
persist('token', token)
state.token = token
}
}
Expand All @@ -29,8 +31,6 @@ export const actions = {
.$post('/api/auth', { username, password })
.then(({ token, user }) => {
this.$axios.setToken(token, 'Bearer')
persist('token', token)
persist('user', user)
commit('setToken', token)
return commit('setUser', user)
})
Expand Down

0 comments on commit 6941add

Please sign in to comment.