"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { useLanguage } from "@/contexts/LanguageContext"; import { useAuth } from "@/contexts/AuthContext"; import { ApiError } from "@/lib/api"; import { toast } from "sonner"; export default function Auth() { const { t } = useLanguage(); const { login, register, user } = useAuth(); const router = useRouter(); const [isLogin, setIsLogin] = useState(true); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [name, setName] = useState(""); const [loading, setLoading] = useState(false); useEffect(() => { if (user) { router.replace(user.role === "admin" ? "/admin" : "/"); } }, [user, router]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { if (isLogin) { const profile = await login(email, password); toast.success(t("auth.login_success")); router.push(profile.role === "admin" ? "/admin" : "/"); } else { const profile = await register(email, password, name); if (profile) { toast.success(t("auth.register_success")); router.push(profile.role === "admin" ? "/admin" : "/"); } else { toast.success(t("auth.confirm_email")); router.push("/connexion"); } } } catch (err) { const msg = err instanceof ApiError ? err.message : t("auth.error"); toast.error(msg); } finally { setLoading(false); } }; return (
{isLogin ? t("auth.login_subtitle") : t("auth.register_subtitle")}
{isLogin ? t("auth.no_account") : t("auth.has_account")}{" "}