"use client"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useState, type FormEvent } from "react"; import Icon from "@/components/Icon"; import Logo from "@/components/Logo"; import { useToast } from "@/components/Toast"; import { api } from "@/lib/client"; import { DEMO_EMAIL, DEMO_PASSWORD } from "@/lib/demo"; import { cn, safeInternalPath } from "@/lib/utils"; const inputCls = "w-full rounded-xl border border-slate-200 bg-white px-3.5 py-2.5 text-sm text-slate-900 focus:border-brand-500 focus:outline-none focus:ring-2 focus:ring-brand-500/20 dark:border-[#2e3240] dark:bg-[#14161a] dark:text-[#e8eaed]"; export default function AuthForm({ mode, demoEnabled = false, }: { mode: "login" | "signup"; demoEnabled?: boolean; }) { const router = useRouter(); const { toast } = useToast(); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [showPw, setShowPw] = useState(false); const [error, setError] = useState(null); const [busy, setBusy] = useState<"form" | "demo" | null>(null); const authenticate = async ( trigger: "form" | "demo", path: "login" | "signup", body: Record, ) => { if (busy) return; setError(null); setBusy(trigger); const res = await api(`/api/auth/${path}`, { method: "POST", body }); if (!res.ok) { setError(res.error ?? "Something went wrong. Please try again."); setBusy(null); return; } const session = await api("/api/auth/me"); if (session.status === 401) { setError( "Signed in, but this browser blocked the session cookie. Allow cookies for this site — or open Fox in its own tab if it is embedded in another page — then try again.", ); setBusy(null); return; } toast(path === "login" ? "Welcome back!" : "Account created — welcome to Fox!"); router.push(safeInternalPath(new URLSearchParams(window.location.search).get("redirect"))); router.refresh(); }; const submit = (e: FormEvent) => { e.preventDefault(); void authenticate( "form", mode, mode === "login" ? { email: email.trim(), password } : { name: name.trim(), email: email.trim(), password }, ); }; return (

{mode === "login" ? "Welcome back" : "Create your account"}

{mode === "login" ? "Sign in to see your history, saved results and collections." : "Free forever — save results, build collections, keep your history."}

{mode === "signup" && (
setName(e.target.value)} placeholder="Ada Lovelace" required maxLength={80} className={inputCls} autoComplete="name" />
)}
setEmail(e.target.value)} placeholder="you@example.com" required className={inputCls} autoComplete="email" autoCapitalize="off" dir="ltr" />
setPassword(e.target.value)} placeholder={mode === "signup" ? "At least 8 characters" : "Your password"} required minLength={mode === "signup" ? 8 : undefined} maxLength={200} className={cn(inputCls, "pr-11")} autoComplete={mode === "login" ? "current-password" : "new-password"} dir="ltr" />
{error && (

{error}

)}
{demoEnabled && ( )}

{mode === "login" ? ( <> New to Fox?{" "} Create an account ) : ( <> Already have an account?{" "} Sign in )}

); }