Files
2026-02-02 08:13:15 +07:00

30 lines
1.0 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { defineNuxtRouteMiddleware, navigateTo } from '#app';
export default defineNuxtRouteMiddleware(async () => {
console.log('👤 Guest middleware: Checking if user is already authenticated...');
// Skip on server-side during development
if (process.server && process.env.NODE_ENV === 'development') {
console.log('⏭️ Skipping guest check on server-side during development');
return;
}
try {
console.log('🔍 Checking if user is already authenticated...');
// The $fetch will automatically send the user_session cookie
const session = await $fetch<{ user: any } | null>('/api/auth/session').catch(() => null);
if (session && session.user) {
console.log('✅ User already authenticated, redirecting to dashboard');
return navigateTo('/dashboard');
} else {
console.log(' No session found, staying on login page');
return;
}
} catch (error) {
console.log(' Auth check failed (expected for login), staying on login page');
return;
}
});