"use client"; import { useState } from "react"; import { signOut } from "next-auth/react"; export function EmailForm({ hasPassword }: { hasPassword: boolean }) { const [newEmail, setNewEmail] = useState(""); const [password, setPassword] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const [success, setSuccess] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(""); setSuccess(false); setLoading(true); try { const res = await fetch("/api/profile/email", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ newEmail, password }), }); const data = await res.json(); if (!res.ok) { setError(data.error || "Une erreur est survenue."); } else { setSuccess(true); // Sign out so user logs back in with new email setTimeout(() => signOut({ redirectTo: "/auth/login" }), 2000); } } catch { setError("Une erreur est survenue."); } finally { setLoading(false); } }; if (!hasPassword) { return (

Définissez d'abord un mot de passe ci-dessus.

); } return (
setNewEmail(e.target.value)} placeholder="nouveau@exemple.com" required style={{ fontSize: 14 }} />
setPassword(e.target.value)} placeholder="Votre mot de passe actuel" required autoComplete="current-password" style={{ fontSize: 14 }} />
{error && (

{error}

)} {success && (

✓ Email mis à jour. Reconnexion en cours…

)}
); }