Sauvegarde vers le serveur
This commit is contained in:
@@ -1,20 +1,24 @@
|
||||
// auth.js - Gestion de la connexion SSO
|
||||
|
||||
const authConfig = {
|
||||
authority: "https://idp.a5l.fr/realms/A5L",
|
||||
client_id: "soundboard_a5l", // À enregistrer sur votre IdP
|
||||
redirect_uri: window.location.origin + "/callback.html",
|
||||
response_type: "code",
|
||||
scope: "openid profile email"
|
||||
authority: "https://idp.a5l.fr/realms/A5L",
|
||||
client_id: "soundboard_a5l",
|
||||
// MODIFICATION : pointer vers le fichier PHP
|
||||
redirect_uri: window.location.origin + "/callback.php",
|
||||
response_type: "code",
|
||||
scope: "openid profile email"
|
||||
};
|
||||
|
||||
function login() {
|
||||
// Construction de l'URL exacte attendue par Keycloak
|
||||
const state = generateState();
|
||||
localStorage.setItem('auth_state', state); // Optionnel : pour vérification CSRF
|
||||
|
||||
const authUrl = `${authConfig.authority}/protocol/openid-connect/auth?` +
|
||||
`client_id=${authConfig.client_id}&` +
|
||||
`redirect_uri=${encodeURIComponent(authConfig.redirect_uri)}&` +
|
||||
`response_type=${authConfig.response_type}&` +
|
||||
`scope=${authConfig.scope}&` +
|
||||
`state=${generateState()}`; // Sécurité recommandée
|
||||
`state=${state}`;
|
||||
|
||||
window.location.href = authUrl;
|
||||
}
|
||||
@@ -30,8 +34,10 @@ function generateState() {
|
||||
}
|
||||
|
||||
function logout() {
|
||||
localStorage.removeItem('auth_token');
|
||||
window.location.reload();
|
||||
localStorage.removeItem('auth_token');
|
||||
// On supprime le cookie en le faisant expirer
|
||||
document.cookie = "auth_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
||||
window.location.href = 'index.html';
|
||||
}
|
||||
|
||||
function checkAuth() {
|
||||
@@ -41,4 +47,26 @@ function checkAuth() {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function updateAuthUI() {
|
||||
const token = localStorage.getItem('auth_token');
|
||||
const userNameDisplay = document.getElementById('userNameDisplay');
|
||||
|
||||
if (token && token.includes('.')) {
|
||||
try {
|
||||
// Décodage sécurisé du Base64Url
|
||||
const base64Url = token.split('.')[1];
|
||||
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
|
||||
const payload = JSON.parse(window.atob(base64));
|
||||
|
||||
const username = payload.preferred_username || "Utilisateur";
|
||||
|
||||
if (userNameDisplay) {
|
||||
userNameDisplay.innerText = username.toUpperCase();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Erreur décodage token", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user