owltide/middleware/authenticated.ts
Hornwitser e8226e0062 Implement account type restricted page
Add allowedAccountTypes page metadata which the authenticated middleware
uses to further restrict the types of accounts that can access the page.

If the account type is insufficent to access the page it will return an
HTTP 403 Forbidden status, which is rendered using the error page.
2025-03-09 22:37:07 +01:00

19 lines
483 B
TypeScript

export default defineNuxtRouteMiddleware(async (to, from) => {
const { data: session } = await useAccountSession();
if (!session.value) {
console.log("Not logged in, redirecting to /login");
return navigateTo("/login");
}
if (
to.meta.allowedAccountTypes
&& !to.meta.allowedAccountTypes.includes(session.value.account.type)
) {
throw createError({
status: 403,
statusMessage: "Forbidden",
message: "You are not allowed to access this resource.",
})
}
})