FEAT: Migrate authentication system from Directus to Keycloak, implementing token refresh and enhancing session management
This commit is contained in:
@@ -6,44 +6,32 @@ export default defineNuxtRouteMiddleware(async (to) => {
|
||||
const isAuthRequired = to.meta.auth !== false;
|
||||
|
||||
if (!isAuthRequired) {
|
||||
console.log('[MIDDLEWARE] Auth not required for route:', to.path);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Check Directus auth first
|
||||
const { fetchUser, setUser } = useDirectusAuth();
|
||||
const directusUser = useDirectusUser();
|
||||
|
||||
if (!directusUser.value) {
|
||||
try {
|
||||
const user = await fetchUser();
|
||||
setUser(user.value);
|
||||
} catch (error) {
|
||||
// Directus auth failed, continue to check custom Keycloak auth
|
||||
}
|
||||
}
|
||||
console.log('[MIDDLEWARE] Checking authentication for route:', to.path);
|
||||
|
||||
if (directusUser.value) {
|
||||
// User authenticated with Directus
|
||||
try {
|
||||
// Check Keycloak authentication via session API
|
||||
const sessionData = await $fetch('/api/auth/session') as any;
|
||||
|
||||
console.log('[MIDDLEWARE] Session check result:', {
|
||||
authenticated: sessionData.authenticated,
|
||||
hasUser: !!sessionData.user,
|
||||
userId: sessionData.user?.id
|
||||
});
|
||||
|
||||
if (sessionData.authenticated && sessionData.user) {
|
||||
console.log('[MIDDLEWARE] User authenticated, allowing access');
|
||||
return;
|
||||
}
|
||||
|
||||
// Check custom Keycloak auth via session API
|
||||
try {
|
||||
const sessionData = await $fetch('/api/auth/session') as any;
|
||||
if (sessionData.authenticated && sessionData.user) {
|
||||
// User authenticated with Keycloak
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
// Session check failed, continue to redirect
|
||||
}
|
||||
|
||||
// No authentication found, redirect to login
|
||||
console.log('[MIDDLEWARE] No valid authentication found, redirecting to login');
|
||||
return navigateTo('/login');
|
||||
|
||||
} catch (error) {
|
||||
console.error('Auth middleware error:', error);
|
||||
console.error('[MIDDLEWARE] Auth check failed:', error);
|
||||
return navigateTo('/login');
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user