diff --git a/plugins/axios.js b/plugins/axios.js index d4fa32fb..2f89076e 100644 --- a/plugins/axios.js +++ b/plugins/axios.js @@ -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') + } + }) } diff --git a/store/auth.js b/store/auth.js index 494479fc..78abaa7c 100644 --- a/store/auth.js +++ b/store/auth.js @@ -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 } } @@ -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) })