import React, { useState } from 'react'; import { useAuth } from '../context/AuthContext'; import { useNavigate } from 'react-router-dom'; import { Building2, Mail, Lock, Eye, EyeOff, AlertTriangle } from 'lucide-react'; const Login = () => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); const [authMethod, setAuthMethod] = useState(''); // Pour tracker la méthode d'auth utilisée const navigate = useNavigate(); const { login, loginWithO365, isAuthorized } = useAuth(); const handleSubmit = async (e) => { e.preventDefault(); setIsLoading(true); setError(''); setAuthMethod('local'); try { const success = await login(email, password); if (success) { navigate('/dashboard'); } else { setError('Identifiants incorrects. Veuillez réessayer.'); } } catch (error) { setError(error.message || 'Erreur lors de la connexion'); } setIsLoading(false); }; const handleO365Login = async () => { setIsLoading(true); setError(''); setAuthMethod('o365'); try { // Étape 1 : Login O365 const success = await loginWithO365(); if (!success) { setError("Erreur lors de la connexion Office 365"); setIsLoading(false); return; } // Étape 2 : Récupération du token d’authentification (si ton context le fournit) const token = localStorage.getItem("o365_token"); // ⚠️ Ici j’imagine que tu stockes ton token quelque part (dans ton AuthContext ou localStorage). // Adapte selon ton implémentation de loginWithO365 // Étape 3 : Appel de ton API PHP const response = await fetch("http://localhost/GTA/project/public/php/initial-sync.php", { method: "POST", headers: { "Authorization": `Bearer ${token}`, "Content-Type": "application/json", }, }); const data = await response.json(); console.log("Résultat syncGroups :", data); if (!data.success) { setError("Erreur de synchronisation des groupes : " + data.message); setIsLoading(false); return; } // Étape 4 : Redirection vers le dashboard navigate('/dashboard'); } catch (error) { console.error('Erreur O365:', error); if (error.message?.includes('non autorisé') || error.message?.includes('Accès refusé')) { setError('Accès refusé : Vous devez être membre d\'un groupe autorisé dans votre organisation.'); } else if (error.message?.includes('AADSTS')) { setError('Erreur d\'authentification Azure AD. Contactez votre administrateur.'); } else { setError(error.message || "Erreur lors de la connexion Office 365"); } } setIsLoading(false); }; return (
Gestion de congés
{error.includes('Accès refusé') ? 'Accès refusé' : 'Erreur de connexion'}
{error}
{error.includes('groupe autorisé') && (Contactez votre administrateur pour être ajouté aux groupes appropriés.
)}