"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; export default function RegisterForm() { const [username, setUsername] = useState(""); const [displayName, setDisplayName] = useState(""); const [password, setPassword] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const router = useRouter(); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); setError(""); try { const res = await fetch("/api/register", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password, displayName: displayName.trim() || undefined }) }); if (!res.ok) { const data = await res.json().catch(() => ({})); setError(data.error || "注册失败"); } else { router.replace("/"); router.refresh(); } } finally { setLoading(false); } } return (
{error ?

{error}

: null}
); }