Changeset View
Changeset View
Standalone View
Standalone View
src/resources/vue/components/App.vue
<template> | <template> | ||||
<router-view></router-view> | <router-view v-if="!isLoading"></router-view> | ||||
</template> | </template> | ||||
<script> | <script> | ||||
import store from '../js/store' | |||||
export default { | export default { | ||||
created() { | data() { | ||||
if (localStorage.token) { | return { | ||||
axios.get('/api/auth/info', { | isLoading: true | ||||
headers: { | |||||
Authorization: 'Bearer ' + localStorage.getItem('token') | |||||
} | } | ||||
}).then(response => { | }, | ||||
store.commit('loginUser') | mounted() { | ||||
}).catch(error => { | const token = localStorage.getItem('token') | ||||
if (token) { | |||||
this.$root.startLoading() | |||||
axios.defaults.headers.common.Authorization = 'Bearer ' + token | |||||
axios.get('/api/auth/info') | |||||
.then(response => { | |||||
this.$store.state.authInfo = response.data | |||||
this.isLoading = false | |||||
this.$root.stopLoading() | |||||
this.$root.loginUser(token) | |||||
}) | |||||
.catch(error => { | |||||
this.isLoading = false | |||||
this.$root.stopLoading() | |||||
if (error.response.status === 401 || error.response.status === 403) { | if (error.response.status === 401 || error.response.status === 403) { | ||||
store.commit('logoutUser') | this.$root.logoutUser() | ||||
localStorage.setItem('token', '') | |||||
this.$router.push({name: 'login'}) | |||||
} | |||||
}); | |||||
} | } | ||||
}) | |||||
} else { | |||||
this.$root.stopLoading() | |||||
this.isLoading = false | |||||
} | } | ||||
}, | |||||
} | } | ||||
</script> | </script> |