AgPerry's picture
Add files using upload-large-folder tool
9d4a3b0 verified
Raw History Blame Contribute Delete
6.46 kB
"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<HTMLFormElement>) {
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 (
<main className="auth-page">
<div className="auth-brand"><Brand /></div>
<section className="auth-card auth-status">
<span className="auth-status-icon"><Mail size={28} /></span>
<h1>Verify your email</h1>
<p>We sent a secure LinkedIn verification link to <strong>{pendingEmail}</strong>.</p>
<div className="auth-notice">This verification link expires and can be used once.</div>
{previewUrl && <a className="button button-primary button-full" href={previewUrl}>Open development verification link</a>}
<button className="button button-outline button-full" type="button" disabled={resendState === "sending"} onClick={() => resendVerification(pendingEmail)}>
{resendState === "sending" ? "Sending…" : resendState === "sent" ? <><CheckCircle2 size={17} /> Email sent</> : "Resend email"}
</button>
{error && <div className="form-error" role="alert">{error}</div>}
<div className="auth-switch"><Link prefetch={false} href="/login">Return to sign in</Link></div>
</section>
<footer className="auth-footer">LinkedIn © 2026 · WebsiteBench account · Not affiliated with LinkedIn · Privacy · Terms · Help</footer>
</main>
);
}
return (
<main className="auth-page">
<div className="auth-brand"><Brand /></div>
<section className="auth-card">
<h1>{mode === "login" ? "Sign in" : "Join LinkedIn"}</h1>
<p>{mode === "login" ? "Stay connected to your professional community." : "Make the most of your professional life."}</p>
<form method="post" onSubmit={submit}>
{mode === "register" && (
<>
<label>Full name<input name="name" autoComplete="name" required minLength={2} maxLength={100} /></label>
<label>Username<input name="username" autoComplete="username" required minLength={3} maxLength={32} pattern="[A-Za-z0-9_\\-]+" /></label>
</>
)}
<label>Email<input name="email" type="email" autoComplete="email" required /></label>
<label>Password<input name="password" type="password" autoComplete={mode === "login" ? "current-password" : "new-password"} required minLength={10} /></label>
{mode === "register" && <small>Use 10+ characters with uppercase, lowercase, and a number. Use a password created for this site.</small>}
{error && <div className="form-error" role="alert">{error}</div>}
<button className="button button-primary button-full" disabled={loading}>{loading ? "Please wait…" : mode === "login" ? "Sign in" : "Agree & join"}</button>
</form>
{mode === "login" && pendingEmail && (
<button className="auth-resend" type="button" disabled={resendState === "sending"} onClick={() => resendVerification(pendingEmail)}>
{resendState === "sending" ? "Sending…" : resendState === "sent" ? "Verification email sent" : "Resend verification email"}
</button>
)}
<div className="auth-switch">
{mode === "login" ? <>New to LinkedIn? <Link prefetch={false} href="/register">Join now</Link></> : <>Already on LinkedIn? <Link prefetch={false} href="/login">Sign in</Link></>}
</div>
<ReplicaSafetyNotice />
</section>
<footer className="auth-footer">LinkedIn © 2026 · Privacy · Terms · Help</footer>
</main>
);
}