20 lines
624 B
TypeScript
20 lines
624 B
TypeScript
export default defineNuxtRouteMiddleware(async (to) => {
|
|
const { session, fetchSession } = useAuth();
|
|
|
|
if (!session.value) {
|
|
await fetchSession();
|
|
}
|
|
|
|
const loggedIn = computed(() => !!session.value);
|
|
|
|
// if authenticated, and on a signin/signup page, redirect to home page
|
|
if (to.path.toLowerCase().includes("/auth/") && loggedIn.value) {
|
|
return await navigateTo((to.query.to as string) ?? "/");
|
|
}
|
|
|
|
// If not authenticated, and not on a signin/signup page, redirect to login page
|
|
if (!loggedIn.value && !to.path.toLowerCase().includes("/auth/")) {
|
|
return await navigateTo(`/auth/login?to=${to.path}`);
|
|
}
|
|
});
|