"use client"; import { useState, type FormEvent } from "react"; import { pbLogin, pbRegister, pbSaveAuth } from "@/app/lib/pocketbase"; import styles from "../vip.module.css"; type AuthFormProps = { onSuccess: () => void; autoFocus?: boolean; }; export function AuthForm({ onSuccess, autoFocus }: AuthFormProps) { const [mode, setMode] = useState<"login" | "register">("login"); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [passwordConfirm, setPasswordConfirm] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setError(null); setLoading(true); try { let result; if (mode === "register") { if (password !== passwordConfirm) { setError("两次密码不一致"); setLoading(false); return; } if (password.length < 8) { setError("密码至少 8 位"); setLoading(false); return; } result = await pbRegister(email, password, passwordConfirm); } else { result = await pbLogin(email, password); } pbSaveAuth(result.token, result.record); await fetch("/api/auth/sync-session", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ token: result.token, record: result.record }), credentials: "include", }); onSuccess(); } catch (err) { setError(err instanceof Error ? err.message : "操作失败"); } finally { setLoading(false); } }; return (
setEmail(e.target.value)} placeholder="your@email.com" required autoFocus={autoFocus} disabled={loading} style={{ width: "100%", padding: "0.75rem 1rem", fontSize: "1rem", border: "1px solid var(--border)", borderRadius: "8px", background: "var(--background)", color: "var(--foreground)", marginBottom: "0.75rem", }} /> setPassword(e.target.value)} placeholder={mode === "register" ? "至少 8 位" : "••••••••"} required minLength={mode === "register" ? 8 : 1} disabled={loading} style={{ width: "100%", padding: "0.75rem 1rem", fontSize: "1rem", border: "1px solid var(--border)", borderRadius: "8px", background: "var(--background)", color: "var(--foreground)", marginBottom: mode === "register" ? "0.75rem" : 0, }} /> {mode === "register" && ( setPasswordConfirm(e.target.value)} placeholder="再次输入密码" required disabled={loading} style={{ width: "100%", padding: "0.75rem 1rem", fontSize: "1rem", border: "1px solid var(--border)", borderRadius: "8px", background: "var(--background)", color: "var(--foreground)", marginBottom: "0.75rem", }} /> )} {error && (

{error}

)}

{mode === "login" ? ( <> 还没有账号?{" "} ) : ( <> 已有账号?{" "} )}

); }