feat(auth): enforce login route with global middleware
This commit is contained in:
20
frontend/app/middleware/auth.global.ts
Normal file
20
frontend/app/middleware/auth.global.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
export default defineNuxtRouteMiddleware(async (to) => {
|
||||
const isLoginRoute = to.path === "/login";
|
||||
|
||||
const fetcher = import.meta.server ? useRequestFetch() : $fetch;
|
||||
let authenticated = false;
|
||||
try {
|
||||
await fetcher("/api/auth/session", { method: "GET" });
|
||||
authenticated = true;
|
||||
} catch {
|
||||
authenticated = false;
|
||||
}
|
||||
|
||||
if (!authenticated && !isLoginRoute) {
|
||||
return navigateTo("/login");
|
||||
}
|
||||
|
||||
if (authenticated && isLoginRoute) {
|
||||
return navigateTo("/");
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user