"use client"; import { useState } from "react"; import { KeyRound, Loader2 } from "lucide-react"; export function AccountSecurityPanel() { const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [message, setMessage] = useState(null); const [error, setError] = useState(null); const [saving, setSaving] = useState(false); async function submit(event: React.FormEvent) { event.preventDefault(); setMessage(null); setError(null); if (newPassword.length < 8) { setError("新密码至少需要 8 位。"); return; } if (newPassword !== confirmPassword) { setError("两次输入的新密码不一致。"); return; } setSaving(true); try { const response = await fetch("/api/auth/password/change", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ currentPassword, newPassword, confirmPassword }) }); const payload = await response.json().catch(() => ({})) as { error?: string }; if (!response.ok) throw new Error(payload.error || "密码修改失败。"); setCurrentPassword(""); setNewPassword(""); setConfirmPassword(""); setMessage("密码已修改。"); } catch (requestError) { setError(requestError instanceof Error ? requestError.message : String(requestError)); } finally { setSaving(false); } } return (

修改密码

{message ?
{message}
: null} {error ?
{error}
: null}
); }