2025-06-30 18:58:24 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*/
|
2025-03-09 15:51:00 +01:00
|
|
|
export default defineNuxtRouteMiddleware(async (to, from) => {
|
2025-05-26 13:55:01 +02:00
|
|
|
const event = useRequestEvent();
|
|
|
|
const sessionStore = useSessionStore();
|
|
|
|
await callOnce("fetch-session", async () => {
|
|
|
|
await sessionStore.fetch(event);
|
|
|
|
})
|
2025-03-07 18:43:24 +01:00
|
|
|
|
2025-05-26 13:55:01 +02:00
|
|
|
const accountStore = useAccountStore();
|
2025-05-24 20:01:23 +02:00
|
|
|
if (!accountStore.valid) {
|
2025-03-07 18:43:24 +01:00
|
|
|
console.log("Not logged in, redirecting to /login");
|
|
|
|
return navigateTo("/login");
|
|
|
|
}
|
2025-03-09 22:21:13 +01:00
|
|
|
|
|
|
|
if (
|
|
|
|
to.meta.allowedAccountTypes
|
2025-05-24 20:01:23 +02:00
|
|
|
&& !to.meta.allowedAccountTypes.includes(accountStore.type!)
|
2025-03-09 22:21:13 +01:00
|
|
|
) {
|
|
|
|
throw createError({
|
|
|
|
status: 403,
|
|
|
|
statusMessage: "Forbidden",
|
|
|
|
message: "You are not allowed to access this resource.",
|
|
|
|
})
|
|
|
|
}
|
2025-03-07 18:43:24 +01:00
|
|
|
})
|