let API_BASE_URL; const API_BASE_URL = window.location.origin + '/api'; // Gestion du token function getToken() { return localStorage.getItem('authToken'); } function setToken(token) { localStorage.setItem('authToken', token); } function removeToken() { localStorage.removeItem('authToken'); localStorage.removeItem('currentUser'); } // Fonction pour faire des requêtes API async function apiCall(endpoint, method = 'GET', data = null) { const options = { method: method, headers: { 'Content-Type': 'application/json' } }; const token = getToken(); if (token) { options.headers['Authorization'] = `Bearer ${token}`; } if (data && (method === 'POST' || method === 'PUT')) { options.body = JSON.stringify(data); } try { const response = await fetch(`${API_BASE_URL}/${endpoint}`, options); const result = await response.json(); if (!response.ok) { throw new Error(result.error || 'Erreur serveur'); } return result; } catch (error) { console.error('API Error:', error); throw error; } } // Inscription async function register() { const username = document.getElementById('regUsername').value; const email = document.getElementById('regEmail').value; const password = document.getElementById('regPassword').value; if (!username || !email || !password) { alert('Veuillez remplir tous les champs'); return; } try { const result = await apiCall('auth.php?action=register', 'POST', { username, email, password }); setToken(result.token); localStorage.setItem('currentUser', JSON.stringify(result.user)); alert('Compte créé avec succès!'); closeModal(); if (result.user.role === 'admin') { window.location.href = 'admin.html'; } else { window.location.href = 'dashboard.html'; } } catch (error) { alert(error.message); } } // Connexion async function login() { const username = document.getElementById('loginUsername').value; const password = document.getElementById('loginPassword').value; if (!username || !password) { alert('Veuillez remplir tous les champs'); return; } try { const result = await apiCall('auth.php?action=login', 'POST', { username, password }); setToken(result.token); localStorage.setItem('currentUser', JSON.stringify(result.user)); if (result.user.role === 'admin') { window.location.href = 'admin.html'; } else { window.location.href = 'dashboard.html'; } } catch (error) { alert(error.message); } } // Déconnexion function logout() { removeToken(); window.location.href = 'index.html'; } // Afficher/Masquer le modal function showModal(type) { document.getElementById('authModal').style.display = 'block'; if (type === 'register') { toggleAuth('register'); } } function closeModal() { document.getElementById('authModal').style.display = 'none'; } function toggleAuth(type) { if (type === 'login') { document.getElementById('loginForm').style.display = 'block'; document.getElementById('registerForm').style.display = 'none'; } else { document.getElementById('loginForm').style.display = 'none'; document.getElementById('registerForm').style.display = 'block'; } } // Vérifier si l'utilisateur est connecté au chargement window.onload = function() { const token = getToken(); const user = localStorage.getItem('currentUser'); if (token && user) { const userData = JSON.parse(user); if (userData.role === 'admin') { window.location.href = 'admin.html'; } else { window.location.href = 'dashboard.html'; } } }; // Fermer modal en cliquant dehors window.onclick = function(event) { const modal = document.getElementById('authModal'); if (event.target === modal) { closeModal(); } }