"use client"; import { useState } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import Link from "next/link"; import { CheckCircle2, Mail } from "lucide-react"; import { Brand } from "./Brand"; import { ReplicaSafetyNotice } from "./ReplicaSafetyNotice"; type AuthResult = { error?: string; code?: string; requiresVerification?: boolean; verificationPreviewUrl?: string; }; export function AuthForm({ mode }: { mode: "login" | "register" }) { const router = useRouter(); const searchParams = useSearchParams(); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const [pendingEmail, setPendingEmail] = useState(""); const [previewUrl, setPreviewUrl] = useState(""); const [resendState, setResendState] = useState<"idle" | "sending" | "sent">("idle"); function nextDestination() { const next = searchParams.get("next"); if (next && next.startsWith("/") && !next.startsWith("//")) return next; return "/feed"; } async function resendVerification(email: string) { setResendState("sending"); setError(""); try { const response = await fetch("/api/auth/verification/resend", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ email }), }); const result = await response.json() as AuthResult; if (!response.ok) throw new Error(result.error ?? "Unable to resend the verification email."); if (result.verificationPreviewUrl) setPreviewUrl(result.verificationPreviewUrl); setResendState("sent"); } catch (caught) { setError(caught instanceof Error ? caught.message : "Unable to resend the verification email."); setResendState("idle"); } } async function submit(event: React.FormEvent) { event.preventDefault(); setError(""); setLoading(true); const form = new FormData(event.currentTarget); const payload = Object.fromEntries(form.entries()); try { const response = await fetch(`/api/auth/${mode}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(payload), }); const result = await response.json() as AuthResult; if (!response.ok) { if (mode === "login" && result.code === "email_unverified") { setPendingEmail(String(payload.email ?? "")); } throw new Error(result.error ?? "Unable to continue."); } if (mode === "register" && result.requiresVerification) { setPendingEmail(String(payload.email ?? "")); setPreviewUrl(result.verificationPreviewUrl ?? ""); return; } router.push(mode === "login" ? nextDestination() : "/feed"); router.refresh(); } catch (caught) { setError(caught instanceof Error ? caught.message : "Unable to continue."); } finally { setLoading(false); } } if (mode === "register" && pendingEmail) { return (

Verify your email

We sent a secure LinkedIn verification link to {pendingEmail}.

This verification link expires and can be used once.
{previewUrl && Open development verification link} {error &&
{error}
}
Return to sign in
LinkedIn © 2026 · WebsiteBench account · Not affiliated with LinkedIn · Privacy · Terms · Help
); } return (

{mode === "login" ? "Sign in" : "Join LinkedIn"}

{mode === "login" ? "Stay connected to your professional community." : "Make the most of your professional life."}

{mode === "register" && ( <> )} {mode === "register" && Use 10+ characters with uppercase, lowercase, and a number. Use a password created for this site.} {error &&
{error}
}
{mode === "login" && pendingEmail && ( )}
{mode === "login" ? <>New to LinkedIn? Join now : <>Already on LinkedIn? Sign in}
); }