update user login baru dan hakakses
This commit is contained in:
No files matched your search
+18
-8
@@ -1,13 +1,27 @@
|
||||
import { defineNuxtRouteMiddleware, navigateTo } from '#app';
|
||||
import type { RouteLocationNormalized } from 'vue-router';
|
||||
// Import the useAuth composable (it will be auto-imported, but this helps TypeScript/VS Code)
|
||||
import { useAuth } from '~/composables/useAuth';
|
||||
|
||||
// NOTE: We don't need the interfaces here anymore, as useAuth handles the typing.
|
||||
|
||||
export default defineNuxtRouteMiddleware(async (to: RouteLocationNormalized) => {
|
||||
console.log('🛡️ Auth middleware triggered for:', to.path);
|
||||
|
||||
// Check for bypass flag (from keyboard shortcut)
|
||||
if (process.client) {
|
||||
const bypassFlag = sessionStorage.getItem('bypassRootRedirect');
|
||||
if (bypassFlag === 'true' && to.path === '/') {
|
||||
console.log('🔑 Bypass flag detected - allowing root access');
|
||||
// Clear the flag immediately to prevent future bypasses
|
||||
sessionStorage.removeItem('bypassRootRedirect');
|
||||
return; // Allow access without any redirect
|
||||
}
|
||||
}
|
||||
|
||||
// Redirect root path to LoginPage
|
||||
if (to.path === '/') {
|
||||
console.log('🔄 Redirecting from root to LoginPage');
|
||||
return navigateTo('/LoginPage', { replace: true });
|
||||
}
|
||||
|
||||
// Allow the login page to handle its own checks without redirection loops
|
||||
if (to.path === '/LoginPage') {
|
||||
console.log('⏭️ Allowing access to LoginPage');
|
||||
@@ -15,10 +29,8 @@ export default defineNuxtRouteMiddleware(async (to: RouteLocationNormalized) =>
|
||||
}
|
||||
|
||||
// Skip auth check if it's the development server side render pass.
|
||||
// Although we still recommend using the checkAuth() composable for better SSR handling.
|
||||
if (process.server && process.env.NODE_ENV === 'development') {
|
||||
console.log('⏭️ Skipping intensive check on server-side during development');
|
||||
// Using the composable here ensures it is registered correctly, even if we skip the main check
|
||||
useAuth();
|
||||
return;
|
||||
}
|
||||
@@ -35,7 +47,6 @@ export default defineNuxtRouteMiddleware(async (to: RouteLocationNormalized) =>
|
||||
const { checkAuth } = useAuth();
|
||||
console.log('🔍 Checking authentication status using useAuth...');
|
||||
|
||||
// Use the composable's function to check the session
|
||||
const user = await checkAuth();
|
||||
|
||||
if (user) {
|
||||
@@ -43,7 +54,6 @@ export default defineNuxtRouteMiddleware(async (to: RouteLocationNormalized) =>
|
||||
return;
|
||||
} else {
|
||||
console.log('❌ No valid session found, redirecting to login');
|
||||
// If checkAuth fails, it already cleared the user state. Redirect.
|
||||
return navigateTo('/LoginPage');
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -51,4 +61,4 @@ export default defineNuxtRouteMiddleware(async (to: RouteLocationNormalized) =>
|
||||
console.log('🔄 Redirecting to login due to error');
|
||||
return navigateTo('/LoginPage');
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user