File size: 6,457 Bytes
9d4a3b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"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>
  );
}