Actualiser js/auth.js

This commit is contained in:
2026-07-03 21:03:08 +02:00
parent ee20880669
commit e0aa3a8b51
+13 -29
View File
@@ -15,37 +15,21 @@ function removeToken() {
localStorage.removeItem('currentUser'); localStorage.removeItem('currentUser');
} }
// Fonction pour faire des requêtes API // Remplace le bloc try/catch de apiCall par celui-ci pour mieux voir l'erreur
async function apiCall(endpoint, method = 'GET', data = null) { try {
const options = { const response = await fetch(`${API_BASE_URL}/${endpoint}`, options);
method: method, const text = await response.text(); // On récupère le texte brut d'abord
headers: { console.log('Réponse brute du serveur :', text); // <--- AJOUTE ÇA
'Content-Type': 'application/json'
}
};
const token = getToken(); const result = JSON.parse(text); // On tente le parse après
if (token) {
options.headers['Authorization'] = `Bearer ${token}`; if (!response.ok) {
} throw new Error(result.error || 'Erreur serveur');
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;
} }
return result;
} catch (error) {
console.error('API Error:', error);
throw error;
} }
// Inscription // Inscription