Actualiser js/auth.js
This commit is contained in:
+7
-23
@@ -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'
|
||||
}
|
||||
};
|
||||
|
||||
const token = getToken();
|
||||
if (token) {
|
||||
options.headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
if (data && (method === 'POST' || method === 'PUT')) {
|
||||
options.body = JSON.stringify(data);
|
||||
}
|
||||
|
||||
try {
|
||||
// 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 result = await response.json();
|
||||
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 result = JSON.parse(text); // On tente le parse après
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(result.error || 'Erreur serveur');
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
} catch (error) {
|
||||
console.error('API Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Inscription
|
||||
|
||||
Reference in New Issue
Block a user