19 lines
555 B
TypeScript
19 lines
555 B
TypeScript
export default defineNuxtRouteMiddleware((to) => {
|
|
const { loggedIn, user } = useUserSession();
|
|
|
|
if (!loggedIn.value || !user.value || user.value?.role !== 'ADMIN') {
|
|
const split = to.path.endsWith('/')
|
|
? to.path.substring(0, to.path.length - 1).split('/')
|
|
: to.path.split('/');
|
|
|
|
const localePath = useLocalePath();
|
|
return navigateTo(
|
|
!user.value
|
|
? localePath(`/login?to=${to.path}`)
|
|
: localePath(
|
|
split.length > 1 ? split.slice(0, split.length - 1).join('/') : '/'
|
|
)
|
|
);
|
|
}
|
|
});
|