router.beforeEach((to, from, next) => {
store.dispatch('currentUser/setUserData')
const roles = store.getters['currentUser/getUserRoles'];
if(to.matched.some(record => record.meta.requiresAuth)) {
if (localStorage.getItem('cmdb_token') == null) {
next({
path: '/login',
query: { redirect: to.fullPath }
})
} else {
if(to.matched.some(record => record.meta.requiresAdmin)) {
if(roles.includes('admin')) {
next()
} else {
}
} else if(to.matched.some(record => record.meta.requiresInventory)) {
if(roles.includes('inventory') || roles.includes('admin')){
next()
} else {
next({
path: '/',
query: { redirect: to.fullPath }
})
}
} else if(to.matched.some(record => record.meta.requiresContractor)) {
if(roles.includes('contractor') || roles.includes('admin')){
next()
} else {
next({
path: '/',
query: { redirect: to.fullPath }
})
}
}
}
} else {
next()
}
});
router.beforeEach((to, from, next) => {
store.dispatch('currentUser/setUserData')
const roles = store.getters['currentUser/getUserRoles'];
if(to.matched.some(record => record.meta.requiresAuth)) {
if (localStorage.getItem('cmdb_token') == null) {
next({
path: '/login',
query: { redirect: to.fullPath }
})
} else {
if(to.matched.some(record => record.meta.requiresAdmin)) {
if(roles.includes('admin')) {
next()
} else {
}
} else if(to.matched.some(record => record.meta.requiresInventory)) {
if(roles.includes('inventory') || roles.includes('admin')){
next()
} else {
next({
path: '/',
query: { redirect: to.fullPath }
})
}
} else if(to.matched.some(record => record.meta.requiresContractor)) {
if(roles.includes('contractor') || roles.includes('admin')){
next()
} else {
next({
path: '/',
query: { redirect: to.fullPath }
})
}
}
}
} else {
next()
}
});
router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.requiresAuth)) {
// этот путь требует авторизации, проверяем залогинен ли
// пользователь, и если нет, перенаправляем на страницу логина
if (!auth.loggedIn()) {
next({
path: '/login',
query: { redirect: to.fullPath }
})
} else {
next()
}
} else {
next() // всегда так или иначе нужно вызвать next()!
}
})