"use client"; import { useState } from "react"; import Head from "next/head"; import Alert from "../ui/components/Alert"; import { useRouter } from "next/navigation"; import Link from "next/link"; const SECURITY_QUESTIONS = [ "您的出生地是?", "您的母亲的姓名是?", "您的最喜欢的颜色是?", "您的第一所学校名称是?", "您的宠物名字是?", "自定义问题", ]; export default function ResetPasswordPage() { const router = useRouter(); const [step, setStep] = useState(1); const [username, setUsername] = useState(""); const [selectedQuestion, setSelectedQuestion] = useState( SECURITY_QUESTIONS[0] ); const [customQuestion, setCustomQuestion] = useState(""); const [securityAnswer, setSecurityAnswer] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [alert, setAlert] = useState(null); const handleVerification = async (e) => { e.preventDefault(); try { const response = await fetch("/api/auth/verify-security", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ username, securityQuestion: selectedQuestion === "自定义问题" ? customQuestion.trim() : selectedQuestion, securityAnswer: securityAnswer.trim(), }), }); const data = await response.json(); if (data.success) { setAlert({ type: "success", message: "验证成功" }); setStep(2); } else { setAlert({ type: "error", message: data.error || "验证失败,请重试", }); } } catch (error) { setAlert({ type: "error", message: "发生错误,请稍后重试" }); } }; const handleResetPassword = async (e) => { e.preventDefault(); if (newPassword.length < 6) { setAlert({ type: "error", message: "密码至少需要6个字符" }); return; } if (newPassword !== confirmPassword) { setAlert({ type: "error", message: "两次输入的密码不一致" }); return; } try { const response = await fetch("/api/auth/reset-password", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ username, newPassword, }), }); const data = await response.json(); if (data.success) { setAlert({ type: "success", message: "密码重置成功" }); setTimeout(() => router.push("/login"), 1500); } else { setAlert({ type: "error", message: data.message || "密码重置失败,请重试", }); } } catch (error) { setAlert({ type: "error", message: "发生错误,请稍后重试" }); } }; return (
重置密码 {/*

重置密码

*/}
{step === 1 ? (
setUsername(e.target.value)} />
{selectedQuestion === "自定义问题" && (
setCustomQuestion(e.target.value)} required />
)}
setSecurityAnswer(e.target.value)} required />
) : (
setNewPassword(e.target.value)} />
setConfirmPassword(e.target.value)} />
)}
{step === 1 ? "记起密码了?" : "需要重新验证?"}
{step === 1 ? ( 返回登录 ) : ( )}
{alert && ( setAlert(null)} /> )}
); }