// Client API pour interagir avec le backend const api = { // Vérifier l'authentification async checkAuth() { try { const response = await fetch('/api/stats'); if (response.status === 401) { window.location.href = '/login.html'; return false; } return response.ok; } catch (error) { console.error('Erreur de vérification d\'authentification:', error); return false; } }, // Récupérer la configuration async getConfig() { const response = await fetch('/api/config'); if (response.status === 401) { window.location.href = '/login.html'; throw new Error('Non authentifié'); } if (!response.ok) { throw new Error('Erreur de récupération de la configuration'); } return await response.json(); }, // Enregistrer la configuration async setConfig(config) { const response = await fetch('/api/config', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: 'config=' + encodeURIComponent(JSON.stringify(config)) }); if (response.status === 401) { window.location.href = '/login.html'; throw new Error('Non authentifié'); } if (!response.ok) { throw new Error('Erreur d\'enregistrement de la configuration'); } return await response.json(); }, // Changer le mot de passe async changePassword(config) { const response = await fetch('/api/password', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: 'config=' + encodeURIComponent(JSON.stringify(config)) }); if (response.status === 401) { throw new Error('Non authentifié ou mot de passe incorrect'); } if (!response.ok) { const error = await response.json(); throw new Error(error.message || 'Erreur de changement de mot de passe'); } return await response.json(); }, // Récupérer les statistiques async getStats() { const response = await fetch('/api/stats'); if (response.status === 401) { window.location.href = '/login.html'; throw new Error('Non authentifié'); } if (!response.ok) { throw new Error('Erreur de récupération des statistiques'); } return await response.json(); }, // Déconnexion async logout() { const response = await fetch('/logout'); if (response.ok) { window.location.href = '/login.html'; } } }; // Vérifier l'authentification au chargement de la page (sauf sur login.html) if (!window.location.pathname.includes('login.html')) { api.checkAuth(); }