diff --git a/js/auth.js b/js/auth.js index 5eec8e8..c71a8e4 100644 --- a/js/auth.js +++ b/js/auth.js @@ -15,37 +15,21 @@ function removeToken() { 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' - } - }; +// Remplace le bloc try/catch de apiCall par celui-ci pour mieux voir l'erreur +try { + const response = await fetch(`${API_BASE_URL}/${endpoint}`, options); + const text = await response.text(); // On récupère le texte brut d'abord + console.log('Réponse brute du serveur :', text); // <--- AJOUTE ÇA - const token = getToken(); - if (token) { - options.headers['Authorization'] = `Bearer ${token}`; - } + const result = JSON.parse(text); // On tente le parse après - 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; + if (!response.ok) { + throw new Error(result.error || 'Erreur serveur'); } + return result; +} catch (error) { + console.error('API Error:', error); + throw error; } // Inscription