fix(frontend): update router guard API
This commit is contained in:
@@ -244,30 +244,32 @@ const router = createRouter({
|
||||
});
|
||||
|
||||
// Navigation guards
|
||||
router.beforeEach((to, from, next) => {
|
||||
router.beforeEach((to) => {
|
||||
const authStore = useAuthStore();
|
||||
|
||||
if (to.matched.some(record => record.meta.requiresAuth)) {
|
||||
if (!authStore.isAuthenticated) {
|
||||
next({
|
||||
return {
|
||||
name: 'login',
|
||||
query: { returnUrl: to.fullPath },
|
||||
});
|
||||
} else {
|
||||
const requiredRoles = to.matched.flatMap(record => record.meta.roles ?? []);
|
||||
if (requiredRoles.length > 0 && !authStore.hasAnyRole(requiredRoles)) {
|
||||
next({ name: 'dashboard' });
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
next();
|
||||
const requiredRoles = to.matched.flatMap(record => record.meta.roles ?? []);
|
||||
if (requiredRoles.length > 0 && !authStore.hasAnyRole(requiredRoles)) {
|
||||
return { name: 'dashboard' };
|
||||
}
|
||||
} else if (to.matched.some(record => record.meta.notAuthenticated)) {
|
||||
if (authStore.isAuthenticated) next({ name: 'dashboard' });
|
||||
else next();
|
||||
} else {
|
||||
next();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (to.matched.some(record => record.meta.notAuthenticated)) {
|
||||
return authStore.isAuthenticated
|
||||
? { name: 'dashboard' }
|
||||
: true;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user