// components/account/PasswordChange.tsx "use client"; import { useState } from "react"; export default function PasswordChange() { const [current, setCurrent] = useState(""); const [next, setNext] = useState(""); const [next2, setNext2] = useState(""); const [busy, setBusy] = useState(false); const [msg, setMsg] = useState(null); const onSave = async () => { setMsg(null); if (next !== next2) { setMsg("New passwords do not match."); return; } if (next.length < 8) { setMsg("Password must be at least 8 characters."); return; } setBusy(true); try { const r = await fetch("/api/account/password", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ current, next }), }); if (r.status === 401) { // Wrong current or expired token; the route returns a friendly message for wrong current. const j = await r.json().catch(() => ({})); setMsg(j?.error || "Re-authentication required."); return; } const j = await r.json().catch(() => ({})); if (!r.ok) { setMsg(j?.error || "Password change failed"); return; } setMsg("Password updated."); setCurrent(""); setNext(""); setNext2(""); } catch (e: any) { setMsg(e?.message || "Password change failed"); } finally { setBusy(false); } }; return (

Change Password

{msg &&
{msg}
}
); }