drop/server/middleware/require-user.global.ts

27 lines
678 B
TypeScript
Raw Permalink Normal View History

2025-04-01 18:18:57 +11:00
const whitelistedPrefixes = ["/auth", "/api", "/setup"];
const requireAdmin = ["/admin"];
2024-10-07 22:35:54 +11:00
2025-04-15 21:43:27 -04:00
export default defineNuxtRouteMiddleware(async (to, _from) => {
const error = useError();
if (error.value !== undefined) return;
2024-10-07 22:35:54 +11:00
if (whitelistedPrefixes.findIndex((e) => to.fullPath.startsWith(e)) != -1)
return;
const user = useUser();
if (user.value === undefined) {
2024-10-07 22:35:54 +11:00
await updateUser();
}
if (!user.value) {
2025-03-22 15:54:43 -04:00
return navigateTo({
path: "/auth/signin",
query: { redirect: to.fullPath },
});
2024-10-07 22:35:54 +11:00
}
if (
requireAdmin.findIndex((e) => to.fullPath.startsWith(e)) != -1 &&
!user.value.admin
) {
return navigateTo({ path: "/" });
}
2024-10-07 22:35:54 +11:00
});