"use client"; import { useState, type FormEvent } from "react"; import { pbSaveAuth } from "@/app/lib/pocketbase"; interface AuthModalProps { isOpen: boolean; onClose: () => void; onSuccess: (email: string) => void; } type AuthResult = { ok: boolean; token?: string; record?: { id: string; email?: string; [key: string]: unknown }; error?: string; message?: string; }; 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("Passwords do not match"); setLoading(false); return; } if (password.length < 8) { setError("Password must be at least 8 characters"); setLoading(false); return; } } const endpoint = mode === "login" ? "/api/auth/login" : "/api/auth/register"; const payload = mode === "login" ? { identity: email, password } : { email, password, passwordConfirm }; const res = await fetch(endpoint, { method: "POST", headers: { "Content-Type": "application/json" }, credentials: "include", body: JSON.stringify(payload), }); const data = (await res.json().catch(() => ({}))) as AuthResult; if (!res.ok || !data?.ok || !data?.token || !data?.record) { throw new Error(data?.error || data?.message || "Authentication failed"); } pbSaveAuth(data.token, { id: data.record.id, email: data.record.email ?? email }); onSuccess(email); onClose(); setEmail(""); setPassword(""); setPasswordConfirm(""); } catch (err) { setError(err instanceof Error ? err.message : "Authentication failed"); } finally { setLoading(false); } }; if (!isOpen) return null; return (

{mode === "login" ? "Login" : "Register"}

{mode === "login" ? "Use email and password" : "Create a new account"}

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="At least 8 characters" 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="Enter password again" 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" ? ( <> No account?{" "} ) : ( <> Already have an account?{" "} )}

); }