import { createContext, useContext, useState, useEffect } from 'react'; interface User { id: string; email: string; name: string; } interface AuthContextType { user: User | null; isAuthenticated: boolean; isLoading: boolean; login: (email: string, password: string) => Promise; logout: () => void; } const AuthContext = createContext(undefined); export const useAuth = () => { const context = useContext(AuthContext); if (context === undefined) { throw new Error('useAuth must be used within an AuthProvider'); } return context; }; export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const [user, setUser] = useState(null); const [isLoading, setIsLoading] = useState(true); // Vérifier si l'utilisateur est déjà connecté au chargement useEffect(() => { const savedUser = localStorage.getItem('xpeditis_user'); if (savedUser) { try { setUser(JSON.parse(savedUser)); } catch (error) { localStorage.removeItem('xpeditis_user'); } } setIsLoading(false); }, []); const login = async (email: string, password: string): Promise => { // Simulation d'authentification if (email === 'demo@xpeditis.com' && password === 'demo123') { const userData: User = { id: '1', email: 'demo@xpeditis.com', name: 'Utilisateur Demo' }; setUser(userData); localStorage.setItem('xpeditis_user', JSON.stringify(userData)); return true; } return false; }; const logout = () => { setUser(null); localStorage.removeItem('xpeditis_user'); }; const value = { user, isAuthenticated: !!user, isLoading, login, logout }; return ( {children} ); };