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

@@ -1,6 +1,6 @@
import axios from 'axios';
import { API_CONFIG } from '../config/api';
import { keycloak, KEYCLOAK_TIMEOUTS } from '../config/keycloak';
import keycloak from '../lib/keycloak';
class ApiService {
private api = axios.create({
@@ -16,8 +16,8 @@ class ApiService {
// Vérifier si Keycloak est initialisé et l'utilisateur authentifié
if (keycloak && keycloak.authenticated) {
try {
// Rafraîchir le token si nécessaire
await keycloak.updateToken(KEYCLOAK_TIMEOUTS.TOKEN_REFRESH_BEFORE_EXPIRY);
// Rafraîchir le token si nécessaire (70 secondes avant expiration)
await keycloak.updateToken(70);
// Ajouter le token Bearer à l'en-tête Authorization
if (keycloak.token) {
@@ -29,22 +29,6 @@ class ApiService {
keycloak.login();
throw error;
}
} else {
// Fallback vers l'ancien système pour la rétrocompatibilité
let token = null;
try {
const authTokenItem = sessionStorage.getItem('auth_token') || localStorage.getItem('auth_token');
if (authTokenItem) {
const parsed = JSON.parse(authTokenItem);
token = parsed.value;
}
} catch (e) {
token = localStorage.getItem('token');
}
if (token) {
config.headers['Authorization'] = `Bearer ${token}`;
}
}
return config;
},
@@ -73,12 +57,10 @@ class ApiService {
const hasAuthCode = currentUrl.includes('code=') && currentUrl.includes('/dashboard');
if (!hasAuthCode) {
// Fallback vers l'ancien système
localStorage.removeItem('token');
localStorage.removeItem('user');
localStorage.removeItem('auth_token');
sessionStorage.removeItem('auth_token');
window.location.href = '/api/auth/login';
console.log('❌ Non authentifié, redirection vers Keycloak...');
if (keycloak) {
keycloak.login();
}
} else {
console.log('🔄 ApiService: Erreur 401 ignorée car authentification en cours...');
}