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.
19 lines
483 B
TypeScript
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.",
|
|
})
|
|
}
|
|
})
|