"use client"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useSession } from "next-auth/react"; import { useActionState, useEffect, useState } from "react"; import { AuthForm } from "@/components/auth-form"; import { SubmitButton } from "@/components/submit-button"; import { toast } from "@/components/toast"; import { type RegisterActionState, register } from "../actions"; export default function Page() { const router = useRouter(); const [email, setEmail] = useState(""); const [isSuccessful, setIsSuccessful] = useState(false); const [siteName, setSiteName] = useState("AI Chatbot"); const [siteLogo, setSiteLogo] = useState(null); useEffect(() => { // 获取认证设置并检查是否开放注册 fetch("/api/auth-settings") .then((res) => res.json()) .then((data) => { if (data.isRegistrationEnabled === false) { toast({ type: "error", description: "当前系统未开放注册" }); router.push("/login"); } }) .catch(console.error); // 获取网站设置 fetch("/api/site-settings") .then((res) => res.json()) .then((data) => { if (data.siteName) setSiteName(data.siteName); if (data.siteLogo) setSiteLogo(data.siteLogo); }) .catch(console.error); }, []); const [state, formAction] = useActionState( register, { status: "idle", } ); const { update: updateSession } = useSession(); // biome-ignore lint/correctness/useExhaustiveDependencies: router and updateSession are stable refs useEffect(() => { if (state.status === "user_exists") { toast({ type: "error", description: "账号已存在" }); } else if (state.status === "failed") { toast({ type: "error", description: "创建账号失败" }); } else if (state.status === "invalid_data") { toast({ type: "error", description: "提交的信息校验失败", }); } else if (state.status === "success") { toast({ type: "success", description: "账号创建成功" }); setIsSuccessful(true); updateSession(); router.refresh(); } }, [state.status]); const handleSubmit = (formData: FormData) => { setEmail(formData.get("email") as string); formAction(formData); }; return (
{siteLogo ? ( {siteName} ) : null}

注册 {siteName}

使用邮箱与密码创建新账号

注册

已经有账号? 立即登录 。

); }