Actualiser js/auth.js
This commit is contained in:
+13
-29
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user