The authenticated middleware runs before the page content is rendered. This means that it'll run before the session is fetched in app.vue on pages that are protected by it on the first load. Fetch the session in the middleware so that it doesn't act before the session is initalized.
24 lines
628 B
TypeScript
24 lines
628 B
TypeScript
export default defineNuxtRouteMiddleware(async (to, from) => {
|
|
const event = useRequestEvent();
|
|
const sessionStore = useSessionStore();
|
|
await callOnce("fetch-session", async () => {
|
|
await sessionStore.fetch(event);
|
|
})
|
|
|
|
const accountStore = useAccountStore();
|
|
if (!accountStore.valid) {
|
|
console.log("Not logged in, redirecting to /login");
|
|
return navigateTo("/login");
|
|
}
|
|
|
|
if (
|
|
to.meta.allowedAccountTypes
|
|
&& !to.meta.allowedAccountTypes.includes(accountStore.type!)
|
|
) {
|
|
throw createError({
|
|
status: 403,
|
|
statusMessage: "Forbidden",
|
|
message: "You are not allowed to access this resource.",
|
|
})
|
|
}
|
|
})
|