"use client"; import { useConvexAuth } from "convex/react"; import { useAuthActions } from "@convex-dev/auth/react"; import { useState, useCallback, useEffect } from "react"; import { useRouter } from "next/navigation"; import { isConvexEnabled } from "@/lib/convex/enabled"; type AuthStep = "signIn" | "signUp"; // 测试账号凭据常量 const TEST_CREDENTIALS = { email: "test@example.com", password: "Test123456", } as const; /** * Convex Auth 登录/注册页面 * * 支持功能: * - 邮箱密码登录 * - 邮箱密码注册 */ export default function AuthPage() { const { isLoading, isAuthenticated } = useConvexAuth(); const { signIn } = useAuthActions(); const router = useRouter(); useEffect(() => { if (!isLoading && isAuthenticated) { router.replace("/"); } }, [isAuthenticated, isLoading, router]); const [flow, setFlow] = useState("signIn"); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [name, setName] = useState(""); const [message, setMessage] = useState<{ type: "success" | "error" | "info"; text: string; } | null>(null); // 执行登录的核心逻辑 const performSignIn = useCallback(async (email: string, password: string, flow: AuthStep) => { setMessage(null); try { const formData = new FormData(); formData.append("email", email); formData.append("password", password); if (flow === "signUp") { formData.append("name", name); } formData.append("flow", flow); const result = await signIn("password", formData); if (result.signingIn) { setMessage({ type: "success", text: flow === "signIn" ? "登录成功!" : "注册成功!" }); setTimeout(() => router.push("/"), 500); return; } // 说明:部分认证方式会返回 redirect(例如 OAuth);密码模式一般不会走到这里。 if (result.redirect) { setMessage({ type: "info", text: "正在跳转..." }); router.push(result.redirect.toString()); return; } setMessage({ type: "error", text: "操作失败,请重试" }); } catch (error: any) { setMessage({ type: "error", text: error.message || "操作失败,请重试" }); } }, [name, signIn, router]); const handleSubmit = useCallback(async (event: React.FormEvent) => { event.preventDefault(); await performSignIn(email, password, flow); }, [email, password, flow, performSignIn]); // 检查是否启用了 Convex const isConvex = isConvexEnabled(); if (!isConvex) { return (

认证功能不可用

请启用 Convex 后端后再试

); } if (isLoading) { return (

加载中...

); } if (isAuthenticated) { return (

正在跳转...

); } return (

{flow === "signIn" ? "登录账户" : "创建账户"}

使用 Convex Auth

{message && (

{message.text}

)}
setEmail(e.target.value)} className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-t-md focus:outline-none focus:ring-blue-500 focus:border-blue-500 focus:z-10 sm:text-sm" placeholder="邮箱地址" />
{flow === "signUp" && (
setName(e.target.value)} className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-blue-500 focus:border-blue-500 focus:z-10 sm:text-sm" placeholder="姓名(可选)" />
)}
setPassword(e.target.value)} className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-b-md focus:outline-none focus:ring-blue-500 focus:border-blue-500 focus:z-10 sm:text-sm" placeholder="密码(至少 8 位)" />
{flow === "signIn" && (
)}
); }