router.beforeEach((to, from, next) =>{
if (!store.getters['user/isLogged'] && to.path !== '/login'){
// попытаться восстановить по данным из локалстораджа/куков/пр.
store.dispatch('user/restoreAuth').then(() =>{
if (!store.getters['user/isLogged']) next('/login');
else next();
});
}
next();
});
router.beforeEach((to, from, next) =>{
// остальные проверки по ролям и пр.
});
import store from '@/store';
const routes = [
{ path: '/login', component: LoginPage },
{
path: '/', redirect: (to) => {
const roles = store.getters['user/getRoles'];
if (roles.includes('admin')) {
return '/admin';
}
else {
return '/home';
}
},
},
{
path: '/manager',
component: ManagerPage,
meta: { requireManager: true },
children: [
{
path: ':action',
component: ManagerCRUDComponent,
meta: { requireEdit: true },
props: (route) => {
if (route.query.id) {
return ({ action: route.params.action, id: +route.query.id });
} else {
return ({ action: route.params.action });
}
},
},
],
}];
router.beforeEach((to, from, next) => {
const roles = store.getters['user/getRoles'];
if (roles.includes('admin')) {
next();
} else {
if (to.matched.some((r) => r.meta.requireManager)) {
if (roles.includes('manager')) {
next();
} else {
next(false);
}
} else if (to.matched.some((r) => r.meta.requireEdit)) {
if (roles.includes('editor')) {
next();
} else {
next(false);
}
} else {
next();
}
}
});