21 lines
490 B
TypeScript
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("/");
|
|
}
|
|
});
|