owltide/middleware/authenticated.ts

20 lines
467 B
TypeScript
Raw Normal View History

export default defineNuxtRouteMiddleware(async (to, from) => {
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.",
})
}
})