"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; export default function LoginForm() { const [username, setUsername] = 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/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password }) }); if (!res.ok) { const data = await res.json().catch(() => ({})); setError(data.error || "登录失败"); } else { router.replace("/"); router.refresh(); } } finally { setLoading(false); } } return (
{error ?

{error}

: null}
); }