import { useState } from "react"; import { useNavigate } from "react-router-dom"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Lock, User } from "lucide-react"; export default function Login() { const navigate = useNavigate(); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [isLoading, setIsLoading] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(""); if (!username.trim() || !password.trim()) { setError("Veuillez remplir tous les champs"); return; } setIsLoading(true); try { const response = await fetch('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json', }, // Le navigateur doit inclure les cookies dans la requête et accepter le Set-Cookie credentials: 'include', body: JSON.stringify({ username: username, password: password }), }); const data = await response.json(); if (response.ok) { console.log('Connexion réussie:', data.message); // Vous pouvez stocker le rôle si besoin pour l'affichage UI localStorage.setItem('userRole', data.role); localStorage.setItem('isLoggedIn', 'true'); navigate("/"); } else { console.error('Erreur:', data.error); } } catch (error) { console.error('Erreur réseau:', error); } // TODO: Implémenter l'authentification ici console.log("Login attempt:", { username, password }); setIsLoading(false); }; return (
Connexion Connectez-vous pour accéder à l'administration
setUsername(e.target.value)} className="pl-10" disabled={isLoading} />
setPassword(e.target.value)} className="pl-10" disabled={isLoading} />
{error && (

{error}

)}
); }