"use client"; import Link from "next/link"; import { FormEvent, useEffect, useState } from "react"; import { PreviewNotice } from "@/components/ui"; const apiBase = process.env.NEXT_PUBLIC_API_BASE_URL ?? "/api/v1"; export function AuthForm({ mode }: { mode: "login" | "register" }) { const isLogin = mode === "login"; const [csrf, setCsrf] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [confirmation, setConfirmation] = useState(""); const [message, setMessage] = useState(""); const [error, setError] = useState(""); const [busy, setBusy] = useState(false); useEffect(() => { fetch(`${apiBase}/auth/csrf`, { credentials: "include" }) .then((response) => response.json()) .then((body) => setCsrf(body.csrf_token ?? "")) .catch(() => setError("暂时无法连接认证服务")); }, []); async function submit(event: FormEvent) { event.preventDefault(); setError(""); setMessage(""); if (!isLogin && password !== confirmation) { setError("两次输入的密码不一致"); return; } setBusy(true); try { const response = await fetch(`${apiBase}/auth/${isLogin ? "login" : "register"}`, { method: "POST", credentials: "include", headers: { "Content-Type": "application/json", ...(csrf ? { "X-CSRF-Token": csrf } : {}) }, body: JSON.stringify({ email, password }), }); const body = await response.json().catch(() => ({})); if (!response.ok) throw new Error(body.error?.message ?? "请求失败,请稍后重试"); if (isLogin) { window.location.assign("/account"); } else { setMessage("注册成功,请使用新账户登录"); window.setTimeout(() => window.location.assign("/login"), 500); } } catch (submitError) { setError(submitError instanceof Error ? submitError.message : "请求失败,请稍后重试"); } finally { setBusy(false); } } return (
认证服务已接入生产环境。邮箱注册、登录和会话已启用,验证渠道暂未启用。
setEmail(event.target.value)} placeholder="name@example.com" required type="email" value={email} />
setPassword(event.target.value)} placeholder="至少 8 位字符" required type="password" value={password} />
{!isLogin ?
setConfirmation(event.target.value)} placeholder="再次输入密码" required type="password" value={confirmation} />
: null} {error ?

{error}

: null} {message ?

{message}

: null}

{isLogin ? "还没有账户?" : "已有账户?"}{" "}{isLogin ? "去注册" : "去登录"}

); }