"use client"; import { useState, type FormEvent } from "react"; const PB_URL = process.env.NEXT_PUBLIC_POCKETBASE_URL || "https://pocketbase.hackrobot.cn"; interface AuthModalProps { isOpen: boolean; onClose: () => void; onSuccess: (email: string) => void; } export default function AuthModal({ isOpen, onClose, onSuccess }: AuthModalProps) { 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 { if (mode === "register") { if (password !== passwordConfirm) { setError("两次密码不一致"); setLoading(false); return; } if (password.length < 8) { setError("密码至少 8 位"); setLoading(false); return; } const res = await fetch(`${PB_URL}/api/collections/users/records`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password, passwordConfirm, }), }); if (!res.ok) { const err = await res.json(); throw new Error(err?.message || "注册失败"); } // 注册成功后自动登录 const authRes = await fetch(`${PB_URL}/api/collections/users/auth-with-password`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ identity: email, password }), }); if (!authRes.ok) { throw new Error("注册成功,请手动登录"); } const authData = await authRes.json(); if (authData?.token) { localStorage.setItem("pb_token", authData.token); localStorage.setItem("pb_user", JSON.stringify(authData.record)); } } else { const res = await fetch(`${PB_URL}/api/collections/users/auth-with-password`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ identity: email, password }), }); if (!res.ok) { const err = await res.json(); throw new Error(err?.message || "登录失败"); } const data = await res.json(); if (data?.token) { localStorage.setItem("pb_token", data.token); localStorage.setItem("pb_user", JSON.stringify(data.record)); } } onSuccess(email); onClose(); setEmail(""); setPassword(""); setPasswordConfirm(""); } catch (err) { setError(err instanceof Error ? err.message : "操作失败"); } finally { setLoading(false); } }; if (!isOpen) return null; return (

{mode === "login" ? "登录" : "注册"}

{mode === "login" ? "使用邮箱密码登录" : "创建新账号"}

setEmail(e.target.value)} placeholder="your@email.com" required className="w-full rounded-lg border border-slate-200 px-4 py-2.5 text-slate-800 placeholder-slate-400 focus:border-sky-500 focus:outline-none focus:ring-1 focus:ring-sky-500" />
setPassword(e.target.value)} placeholder="至少 8 位" required minLength={mode === "register" ? 8 : 1} className="w-full rounded-lg border border-slate-200 px-4 py-2.5 text-slate-800 placeholder-slate-400 focus:border-sky-500 focus:outline-none focus:ring-1 focus:ring-sky-500" />
{mode === "register" && (
setPasswordConfirm(e.target.value)} placeholder="再次输入密码" required className="w-full rounded-lg border border-slate-200 px-4 py-2.5 text-slate-800 placeholder-slate-400 focus:border-sky-500 focus:outline-none focus:ring-1 focus:ring-sky-500" />
)} {error && (

{error}

)}

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

); }