Skip to content

Commit

Permalink
Change router beforeEach checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Dual-Ice committed May 24, 2020
1 parent f08a0b1 commit 55cbb4e
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/admin/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,32 @@ router.beforeEach(async (to, from, next) => {
const isAuthRequired = to.matched.some(record => record.meta.auth)
const isUserLogged = store.getters["auth/isLogged"]

if (isAuthRequired && !isUserLogged) {
const token = localStorage.getItem('user-token')
axios.defaults.headers['Authorization'] = `Bearer ${ token }`

try {
const response = await axios.get('/user')
store.commit("auth/setUser", response.data.user)
if (!isUserLogged) {
const token = localStorage.getItem('user-token');
if (!token && !isAuthRequired) {
next()
} catch (e) {
await router.replace('/login')
localStorage.removeItem('user-token')
} else if (token) {
axios.defaults.headers['Authorization'] = `Bearer ${ token }`
try {
const response = await axios.get('/user')
store.commit("auth/setUser", response.data.user)
if (from.path === "/login") {
next()
}
next({ path: from.path })
} catch (e) {
localStorage.removeItem('user-token')
next('/login')
}
} else {
next('/login')
}
} else if (isUserLogged && !isAuthRequired) {
next({ path: from.path })
} else {
next()
next ()
}

document.title = to.meta.title || ''
})

Expand Down

0 comments on commit 55cbb4e

Please sign in to comment.