"use client"; import { useState } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import { Button } from "@/components/ui/button"; import { useAuth } from "@/components/auth/auth-provider"; export default function LoginForm() { const router = useRouter(); const searchParams = useSearchParams(); const { refresh } = useAuth(); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(""); setLoading(true); try { const res = await fetch("/api/auth/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password }), }); const data = await res.json(); if (!res.ok) { setError(data.error ?? "登录失败"); return; } await refresh(); const next = searchParams.get("next") || "/liuyao"; router.push(next); router.refresh(); } catch { setError("网络错误,请稍后重试"); } finally { setLoading(false); } } return (
setUsername(e.target.value)} required />
setPassword(e.target.value)} required />
{error &&

{error}

}

登录后可使用六爻算卦、生辰八字、综合测算与测算历史

); }