Files
Web-Antrean/middleware/guest.ts
2025-10-01 11:28:06 +07:00

44 lines
1.6 KiB
TypeScript
Raw 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.
// middleware/guest.ts
export default defineNuxtRouteMiddleware(async (to) => {
console.log('👋 Guest middleware triggered for:', to.path);
// Skip middleware on server-side during build/generation
if (process.server && process.env.NODE_ENV === 'development') {
console.log('⏭️ Skipping guest check on server-side during development');
return;
}
const isAuthRedirect = to.query.authenticated === 'true';
const isLogoutSuccess = to.query.logout === 'success';
// If this is a logout success, allow access to login page
if (isLogoutSuccess) {
console.log('✅ Logout success detected, allowing access to login page');
return;
}
// If this is a redirect from a successful login, we need to wait
if (isAuthRedirect) {
console.log('⏳ Client-side is processing a new login session, waiting for session to be established...');
// We navigate to a clean URL to remove the query parameter from the user's view
return navigateTo({ path: to.path, query: {} }, { replace: true });
}
try {
console.log('🔍 Checking if user is already authenticated...');
// The $fetch will automatically send the new 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;
}
});