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