Replace the convoluted useAccountSession composable with a pinia store that in addition allows for the consolidation of all session related functions to grouped into one module.
19 lines
476 B
TypeScript
19 lines
476 B
TypeScript
export default defineNuxtRouteMiddleware(async (to, from) => {
|
|
const sessionStore = useSessionStore();
|
|
|
|
if (!sessionStore.account) {
|
|
console.log("Not logged in, redirecting to /login");
|
|
return navigateTo("/login");
|
|
}
|
|
|
|
if (
|
|
to.meta.allowedAccountTypes
|
|
&& !to.meta.allowedAccountTypes.includes(sessionStore.account.type)
|
|
) {
|
|
throw createError({
|
|
status: 403,
|
|
statusMessage: "Forbidden",
|
|
message: "You are not allowed to access this resource.",
|
|
})
|
|
}
|
|
})
|