Files
clientsflow/frontend/app/middleware/auth.global.ts
2026-02-23 12:01:03 +07:00

21 lines
490 B
TypeScript

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("/");
}
});