owltide/middleware/authenticated.ts
Hornwitser e8ff87d507
All checks were successful
/ build (push) Successful in 1m19s
/ deploy (push) Successful in 16s
Fetch session in authenticated middleware
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.
2025-05-26 13:58:47 +02:00

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.",
})
}
})