153 lines
7.1 KiB
JavaScript
153 lines
7.1 KiB
JavaScript
import React, { useState, useEffect } from 'react';
|
|
import { useAuth } from '../context/AuthContext';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { AlertTriangle } from 'lucide-react';
|
|
|
|
const Login = () => {
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [error, setError] = useState('');
|
|
|
|
const navigate = useNavigate();
|
|
const { loginWithO365, isAuthorized, isLoading: authLoading } = useAuth();
|
|
|
|
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
|
|
|
|
// ✅ AJOUT : Redirection automatique si déjà connecté (cas retour OAuth mobile)
|
|
useEffect(() => {
|
|
if (isAuthorized && !authLoading) {
|
|
console.log('✅ Utilisateur autorisé détecté, redirection vers dashboard...');
|
|
navigate('/dashboard', { replace: true });
|
|
}
|
|
}, [isAuthorized, authLoading, navigate]);
|
|
|
|
const handleO365Login = async () => {
|
|
setIsLoading(true);
|
|
setError('');
|
|
|
|
try {
|
|
if (isMobile) {
|
|
console.log('🔐 Redirection mobile vers Office 365...');
|
|
await loginWithO365();
|
|
// Ce code ne sera jamais atteint sur mobile car il y a une redirection
|
|
} else {
|
|
const success = await loginWithO365();
|
|
|
|
if (!success) {
|
|
setError("Erreur lors de la connexion Office 365");
|
|
setIsLoading(false);
|
|
return;
|
|
}
|
|
|
|
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 if (error.errorCode === 'user_cancelled') {
|
|
setError('Connexion annulée');
|
|
} else {
|
|
setError(error.message || "Erreur lors de la connexion Office 365");
|
|
}
|
|
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
// ✅ AJOUT : Afficher un loader pendant la vérification de l'auth
|
|
if (authLoading) {
|
|
return (
|
|
<div className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 flex items-center justify-center">
|
|
<div className="text-center">
|
|
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-cyan-600 mx-auto mb-4"></div>
|
|
<p className="text-gray-600">Vérification de la connexion...</p>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 flex flex-col lg:flex-row">
|
|
{/* Image côté gauche */}
|
|
<div className="h-32 lg:h-auto lg:flex lg:w-1/2 bg-cover bg-center"
|
|
style={{ backgroundImage: "url('/assets/ImageEnsup.png')" }}>
|
|
<div className="w-full bg-black bg-opacity-40 flex items-center justify-center p-4">
|
|
</div>
|
|
</div>
|
|
|
|
{/* Formulaire côté droit */}
|
|
<div className="flex-1 lg:w-1/2 flex items-center justify-center p-4 lg:p-8">
|
|
<div className="max-w-md w-full">
|
|
<div className="bg-white rounded-2xl shadow-xl p-6 lg:p-8">
|
|
{/* Logo */}
|
|
<div className="text-center mb-6">
|
|
<img
|
|
src="/assets/GA.svg"
|
|
alt="GTA Logo"
|
|
className="h-36 lg:h-40 w-auto mx-auto"
|
|
/>
|
|
<p className="text-lg lg:text-xl font-semibold mb-6" style={{ color: '#7e5aa2' }}>
|
|
GESTION DES TEMPS ET DES ACTIVITÉS
|
|
</p>
|
|
</div>
|
|
|
|
{/* Bouton Office 365 */}
|
|
<div className="mb-4">
|
|
<button
|
|
data-testid="o365-login-btn"
|
|
onClick={handleO365Login}
|
|
disabled={isLoading}
|
|
type="button"
|
|
className="w-full bg-cyan-600 text-white py-3 rounded-lg font-medium hover:bg-cyan-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center space-x-2"
|
|
>
|
|
{isLoading ? (
|
|
<span>Connexion en cours...</span>
|
|
) : (
|
|
<>
|
|
<svg className="w-5 h-5" viewBox="0 0 21 21" fill="currentColor">
|
|
<path d="M10.5 0L0 7v7l10.5 7L21 14V7L10.5 0zM3.5 8.5L10.5 3l7 5.5v5L10.5 19l-7-5.5v-5z" />
|
|
</svg>
|
|
<span>Se connecter avec Office 365</span>
|
|
</>
|
|
)}
|
|
</button>
|
|
</div>
|
|
|
|
{/* Message d'information */}
|
|
<div className="bg-blue-50 border border-blue-200 rounded-lg p-3 text-center">
|
|
<p className="text-blue-800 text-sm">
|
|
Connectez-vous avec votre compte professionnel ENSUP
|
|
</p>
|
|
</div>
|
|
|
|
{/* Message d'erreur */}
|
|
{error && (
|
|
<div className="p-3 bg-red-50 border border-red-200 rounded-lg mt-4">
|
|
<div className="flex items-start space-x-2">
|
|
<AlertTriangle className="w-5 h-5 text-red-500 flex-shrink-0 mt-0.5" />
|
|
<div className="flex-1">
|
|
<p className="text-red-700 text-sm font-medium">
|
|
{error.includes('Accès refusé') ? 'Accès refusé' : 'Erreur de connexion'}
|
|
</p>
|
|
<p className="text-red-600 text-xs mt-1">{error}</p>
|
|
{error.includes('groupe autorisé') && (
|
|
<p className="text-red-600 text-xs mt-2">
|
|
Contactez votre administrateur pour être ajouté aux groupes appropriés.
|
|
</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Login; |