Authentification fonctionnelle via security.lions.dev

This commit is contained in:
DahoudG
2025-11-01 14:16:20 +00:00
parent a5adb84a62
commit 1d68878601
20 changed files with 387 additions and 1067 deletions

View File

@@ -2,6 +2,7 @@
import React from 'react';
import { useAuth } from '../contexts/AuthContext';
import { useKeycloak } from '../contexts/KeycloakContext';
import LoadingSpinner from './ui/LoadingSpinner';
import { useRouter, useSearchParams, usePathname } from 'next/navigation';
import { useEffect, useRef, useMemo } from 'react';
@@ -18,6 +19,7 @@ const ProtectedLayout: React.FC<ProtectedLayoutProps> = ({
requiredPermissions = []
}) => {
const { isAuthenticated, isLoading, user, hasRole, hasPermission } = useAuth();
const { keycloak } = useKeycloak();
const router = useRouter();
const searchParams = useSearchParams();
const pathname = usePathname();
@@ -26,8 +28,8 @@ const ProtectedLayout: React.FC<ProtectedLayoutProps> = ({
// Vérifier s'il y a un code d'autorisation dans l'URL
// Utiliser useMemo pour éviter les re-rendus inutiles
const hasAuthCode = useMemo(() => {
return searchParams.get('code') !== null && pathname === '/dashboard';
}, [searchParams, pathname]);
return searchParams.get('code') !== null;
}, [searchParams]);
useEffect(() => {
console.log('🔍 ProtectedLayout useEffect:', {
@@ -48,11 +50,17 @@ const ProtectedLayout: React.FC<ProtectedLayoutProps> = ({
// Marquer comme redirigé pour éviter les boucles
redirectedRef.current = true;
// Rediriger vers la page de connexion avec l'URL de retour
const searchParamsStr = searchParams.toString();
const currentPath = pathname + (searchParamsStr ? `?${searchParamsStr}` : '');
console.log('🔒 ProtectedLayout: Redirection vers /api/auth/login');
window.location.href = `/api/auth/login?redirect=${encodeURIComponent(currentPath)}`;
// Rediriger vers Keycloak avec l'URL de retour
console.log('🔒 ProtectedLayout: Redirection vers Keycloak');
if (keycloak) {
const searchParamsStr = searchParams.toString();
const currentPath = pathname + (searchParamsStr ? `?${searchParamsStr}` : '');
const redirectUri = currentPath && currentPath !== '/'
? `${window.location.origin}${currentPath}`
: `${window.location.origin}/dashboard`;
keycloak.login({ redirectUri });
}
} else if (hasAuthCode) {
console.log('🔓 ProtectedLayout: Code d\'autorisation détecté, pas de redirection');
} else if (isAuthenticated) {
@@ -60,7 +68,7 @@ const ProtectedLayout: React.FC<ProtectedLayoutProps> = ({
} else if (isLoading) {
console.log('⏳ ProtectedLayout: Chargement en cours, pas de redirection');
}
}, [isAuthenticated, isLoading, hasAuthCode, pathname]);
}, [isAuthenticated, isLoading, hasAuthCode, pathname, keycloak]);
useEffect(() => {
if (isAuthenticated && user) {