"use client"; import { useState } from "react"; export function PasswordForm({ hasPassword }: { hasPassword: boolean }) { const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = 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); if (newPassword !== confirmPassword) { setError("Les mots de passe ne correspondent pas."); return; } if (newPassword.length < 8) { setError("Le mot de passe doit contenir au moins 8 caractères."); return; } setLoading(true); try { const res = await fetch("/api/profile/password", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ currentPassword: hasPassword ? currentPassword : undefined, newPassword, }), }); const data = await res.json(); if (!res.ok) { setError(data.error || "Une erreur est survenue."); } else { setSuccess(true); setCurrentPassword(""); setNewPassword(""); setConfirmPassword(""); } } catch { setError("Une erreur est survenue."); } finally { setLoading(false); } }; return (
); }