Ajoutez des fichiers projet.
This commit is contained in:
126
project/src/context/AuthContext.jsx
Normal file
126
project/src/context/AuthContext.jsx
Normal file
@@ -0,0 +1,126 @@
|
||||
import React, { createContext, useContext, useState, useEffect } from 'react';
|
||||
|
||||
const AuthContext = createContext();
|
||||
|
||||
export const useAuth = () => {
|
||||
const context = useContext(AuthContext);
|
||||
if (!context) {
|
||||
throw new Error('useAuth must be used within an AuthProvider');
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
||||
export const AuthProvider = ({ children }) => {
|
||||
const [user, setUser] = useState(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
// Vérifier si l'utilisateur est déjà connecté
|
||||
const savedUser = localStorage.getItem('user');
|
||||
if (savedUser) {
|
||||
try {
|
||||
setUser(JSON.parse(savedUser));
|
||||
} catch (error) {
|
||||
console.error('Erreur lors du parsing de l\'utilisateur sauvegardé:', error);
|
||||
localStorage.removeItem('user');
|
||||
}
|
||||
}
|
||||
setIsLoading(false);
|
||||
}, []);
|
||||
|
||||
const login = async (email, password) => {
|
||||
try {
|
||||
// Tester plusieurs URLs possibles selon la configuration locale
|
||||
const possibleUrls = [
|
||||
'http://localhost/GTA/project/public/login.php',
|
||||
'http://localhost:80/GTA/project/public/login.php',
|
||||
'http://localhost/GTA/public/login.php',
|
||||
'http://localhost/public/login.php'
|
||||
];
|
||||
|
||||
let response = null;
|
||||
let lastError = null;
|
||||
|
||||
for (const url of possibleUrls) {
|
||||
try {
|
||||
console.log(' Test URL:', url);
|
||||
response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: email,
|
||||
mot_de_passe: password
|
||||
}),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
console.log(' URL qui fonctionne:', url);
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
console.log(' URL échouée:', url, error.message);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!response || !response.ok) {
|
||||
throw new Error('Aucune URL de connexion accessible');
|
||||
}
|
||||
|
||||
const text = await response.text();
|
||||
console.log(' Réponse brute:', text);
|
||||
|
||||
// Vérifier si la réponse est du JSON valide
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(text);
|
||||
} catch (parseError) {
|
||||
console.error(' Réponse non-JSON:', text.substring(0, 200));
|
||||
throw new Error('Le serveur PHP ne répond pas correctement. Vérifiez que PHP est démarré.');
|
||||
}
|
||||
|
||||
if (data.success) {
|
||||
const userData = {
|
||||
id: data.user.id,
|
||||
name: data.user.prenom + ' ' + data.user.nom,
|
||||
prenom: data.user.prenom,
|
||||
nom: data.user.nom,
|
||||
email: data.user.email,
|
||||
role: data.user.role || 'Employe'
|
||||
};
|
||||
setUser(userData);
|
||||
localStorage.setItem('user', JSON.stringify(userData));
|
||||
return true;
|
||||
} else {
|
||||
console.error(' Échec connexion:', data.message);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Erreur de connexion:', error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
setUser(null);
|
||||
localStorage.removeItem('user');
|
||||
};
|
||||
|
||||
const value = {
|
||||
user,
|
||||
login,
|
||||
logout,
|
||||
isLoading
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={value}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export default AuthContext;
|
||||
Reference in New Issue
Block a user