Refactor the existing scattered code dealing with the account state into a pinia store.
19 lines
467 B
TypeScript
19 lines
467 B
TypeScript
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.",
|
|
})
|
|
}
|
|
})
|