import { useState } from 'react' import { Link, useNavigate, useLocation } from 'react-router-dom' import { loginApi } from '../services/auth' import { useAuth } from '../context/AuthContext' export default function Connexion() { const navigate = useNavigate() const location = useLocation() const { login } = useAuth() const justRegistered = location.state?.registered === true const [form, setForm] = useState({ email: '', password: '' }) const [showPassword, setShowPassword] = useState(false) const [error, setError] = useState('') const [loading, setLoading] = useState(false) const handleChange = (e) => setForm({ ...form, [e.target.name]: e.target.value }) // Navigation pleine page (et non fetch) : le flux OAuth passe par des // redirections vers Google puis retour sur /auth/callback. const handleGoogleLogin = () => { window.location.href = '/api/auth/google' } const handleSubmit = async (e) => { e.preventDefault() setError('') setLoading(true) try { const { token, user } = await loginApi(form) login(user, token) if (user.role === 'ADMIN') navigate('/admin') else if (user.role === 'EMPLOYEE') navigate('/employe') else navigate('/mes-gains') } catch (err) { setError(err.message) } finally { setLoading(false) } } return (

Se connecter

Connexion à votre espace

{justRegistered && (
Compte créé avec succès ! Connectez-vous.
)} {error && (
{error}
)}
Ou

Pas encore de compte ?{' '} S'inscrire

) }