"use client"; import { signIn, useSession } from "next-auth/react"; import { useRouter } from "next/navigation"; import { useState } from "react"; export default function LoginPage() { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const router = useRouter(); const { data: session } = useSession(); // 如果已登录,重定向到仪表板 if (session) { router.push("/dashboard"); return null; } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); try { const result = await signIn("credentials", { email, password, redirect: false, }); if (result?.ok) { router.push("/dashboard"); } else { alert("登录失败,请检查邮箱和密码"); } } catch { alert("登录失败,请重试"); } finally { setIsLoading(false); } }; const handleGoogleSignIn = () => { signIn("google", { callbackUrl: "/dashboard" }); }; return (

登录账户

setEmail(e.target.value)} />
setPassword(e.target.value)} />
还没有账户?注册
); }