monacousa-portal/middleware/auth.ts

30 lines
824 B
TypeScript

export default defineNuxtRouteMiddleware(async (to) => {
// Skip auth for public pages
if (to.meta.auth === false) {
return;
}
// Use the same auth system as the rest of the app
const { isAuthenticated, checkAuth, user } = useAuth();
console.log('🛡️ Auth middleware running for:', to.path);
// Ensure auth is checked if user isn't loaded
if (!user.value) {
console.log('🔄 User not loaded, checking auth...');
await checkAuth();
}
console.log('✅ Auth middleware check:', {
isAuthenticated: isAuthenticated.value,
user: user.value?.email
});
if (!isAuthenticated.value) {
console.log('❌ User not authenticated, redirecting to login');
return navigateTo('/login');
}
console.log('✅ Auth middleware passed, allowing access to:', to.path);
});