-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegisterForm.tsx
More file actions
281 lines (268 loc) · 9.67 KB
/
Copy pathRegisterForm.tsx
File metadata and controls
281 lines (268 loc) · 9.67 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"use client";
import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { csrfFetch } from "@/lib/client-csrf";
import { t, type Locale } from "@/lib/i18n";
type Step = "age" | "email" | "otp";
export function RegisterForm({ locale }: { locale: Locale }) {
const router = useRouter();
const [step, setStep] = useState<Step>("age");
const [ageConfirmed, setAgeConfirmed] = useState<"adult" | "minor" | "">("");
const [email, setEmail] = useState("");
const [otp, setOtp] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const [hydrated, setHydrated] = useState(false);
useEffect(() => {
const id = window.setTimeout(() => setHydrated(true), 0);
return () => window.clearTimeout(id);
}, []);
function continueFromAge() {
if (ageConfirmed === "minor") {
router.push("/minor-support");
return;
}
setStep("email");
}
async function requestOtp() {
setLoading(true);
setError("");
try {
const response = await csrfFetch("/api/auth/request-otp", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ email }),
});
if (!response.ok) {
const body = await response.json().catch(() => ({}));
setError(`Error ${response.status}: ${body.error ?? "unknown"}`);
return;
}
setStep("otp");
} catch {
setError(t(locale, "reg.error.timeout"));
} finally {
setLoading(false);
}
}
async function verify() {
setLoading(true);
setError("");
try {
const response = await csrfFetch("/api/auth/verify-otp", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ email, otp, ageOver18: ageConfirmed === "adult" }),
});
if (!response.ok) {
setError(t(locale, "reg.error.codeNoMatch"));
return;
}
router.push("/submit");
} catch {
setError(t(locale, "reg.error.timeout"));
} finally {
setLoading(false);
}
}
return (
<div>
<ProgressBar step={step} locale={locale} />
{step === "age" && (
<div className="mt-8">
<h2 className="font-display text-[28px] font-normal leading-[1.2] tracking-tight md:text-[36px]">
{t(locale, "reg.age.title")}
</h2>
<p className="muted mt-2 text-sm leading-[1.75]">
{t(locale, "reg.age.sub")}
</p>
<div className="mt-6 space-y-3">
<label
className={`flex cursor-pointer items-start gap-3 rounded-xl border p-4 transition-colors ${
ageConfirmed === "adult"
? "border-[var(--teal)] bg-[var(--teal-soft)]"
: "border-[var(--hairline)] bg-[var(--background)] hover:border-[var(--border)]"
}`}
>
<input
type="radio"
checked={ageConfirmed === "adult"}
onChange={() => setAgeConfirmed("adult")}
className="mt-[3px] h-4 w-4 shrink-0 accent-[var(--teal)]"
/>
<span className="text-[15px] font-semibold leading-[1.4] text-[var(--foreground)]">
{t(locale, "auth.ageAdult")}
</span>
</label>
<label
className={`flex cursor-pointer items-start gap-3 rounded-xl border p-4 transition-colors ${
ageConfirmed === "minor"
? "border-[var(--teal)] bg-[var(--teal-soft)]"
: "border-[var(--hairline)] bg-[var(--background)] hover:border-[var(--border)]"
}`}
>
<input
type="radio"
checked={ageConfirmed === "minor"}
onChange={() => setAgeConfirmed("minor")}
className="mt-[3px] h-4 w-4 shrink-0 accent-[var(--teal)]"
/>
<span className="text-[15px] font-semibold leading-[1.4] text-[var(--foreground)]">
{t(locale, "auth.ageMinor")}
</span>
</label>
</div>
<button
className="btn btn-primary mt-8"
disabled={!ageConfirmed || !hydrated}
onClick={continueFromAge}
type="button"
>
{t(locale, "reg.otp.verifyBtn")}
</button>
</div>
)}
{step === "email" && (
<div className="mt-8">
<h2 className="font-display text-[28px] font-normal leading-[1.2] tracking-tight md:text-[36px]">
{t(locale, "reg.email.title")}
</h2>
<p className="muted mt-2 text-sm leading-[1.75]">
{t(locale, "reg.email.sub")}
</p>
<form
className="mt-8 space-y-4"
onSubmit={(e) => { e.preventDefault(); requestOtp(); }}
>
<div>
<label
htmlFor="email"
className="block text-sm font-semibold text-[var(--foreground)]"
>
{t(locale, "reg.email.label")}
</label>
<input
className="field mt-2"
id="email"
name="email"
onChange={(e) => setEmail(e.target.value)}
required
type="email"
autoComplete="email"
inputMode="email"
value={email}
placeholder="you@example.com"
/>
</div>
{error && <FormError message={error} />}
<button
className="btn btn-primary"
disabled={loading || !hydrated || !email}
type="submit"
>
{loading ? t(locale, "reg.email.sending") : t(locale, "reg.email.sendBtn")}
</button>
</form>
</div>
)}
{step === "otp" && (
<div className="mt-8">
<h2 className="font-display text-[28px] font-normal leading-[1.2] tracking-tight md:text-[36px]">
{t(locale, "reg.otp.title")}
</h2>
<p className="muted mt-2 text-sm leading-[1.75]">
{t(locale, "reg.otp.sub")}{" "}
<span className="font-mono text-[var(--foreground)]">{email}</span>.
</p>
<form
className="mt-8 space-y-4"
onSubmit={(e) => { e.preventDefault(); verify(); }}
>
<div>
<label
htmlFor="otp"
className="block text-sm font-semibold text-[var(--foreground)]"
>
{t(locale, "reg.otp.label")}
</label>
<input
className="field mt-2 font-mono text-center text-2xl tracking-[0.4em]"
id="otp"
inputMode="numeric"
maxLength={6}
onChange={(e) =>
setOtp(e.target.value.replace(/\D/g, "").slice(0, 6))
}
required
value={otp}
autoComplete="one-time-code"
placeholder="000000"
/>
</div>
{error && <FormError message={error} />}
<button
className="btn btn-primary"
disabled={loading || !hydrated || otp.length !== 6}
type="submit"
>
{loading ? t(locale, "reg.otp.verifying") : t(locale, "reg.otp.verifyBtn")}
</button>
<p className="muted text-sm leading-[1.7]">
{t(locale, "reg.otp.wrongEmail")}{" "}
<button
type="button"
onClick={() => { setStep("email"); setOtp(""); setError(""); }}
className="link-underline text-[var(--foreground)]"
>
{t(locale, "reg.otp.changeEmail")}
</button>
.
</p>
</form>
</div>
)}
</div>
);
}
function ProgressBar({ step, locale }: { step: Step; locale: Locale }) {
const steps = [t(locale, "reg.progress.age"), t(locale, "reg.progress.email"), t(locale, "reg.progress.otp")];
const current = step === "age" ? 0 : step === "email" ? 1 : 2;
return (
<div className="flex items-center gap-2">
{steps.map((label, i) => (
<div key={label} className="flex items-center gap-2 flex-1 last:flex-none">
<div className="flex items-center gap-2">
<div
className={`flex h-6 w-6 shrink-0 items-center justify-center rounded-full text-[10px] font-semibold transition-colors ${
i < current
? "bg-[var(--teal)] text-white"
: i === current
? "border-2 border-[var(--teal)] text-[var(--teal)]"
: "border border-[var(--border)] text-[var(--muted)]"
}`}
>
{i < current ? (
<svg width="10" height="8" viewBox="0 0 10 8" fill="none">
<path d="M1 4l2.5 2.5L9 1" stroke="white" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
</svg>
) : (
i + 1
)}
</div>
<span className={`hidden text-[11px] sm:block ${i === current ? "font-semibold text-[var(--foreground)]" : "text-[var(--muted)]"}`}>
{label}
</span>
</div>
{i < steps.length - 1 && (
<div className="flex-1 h-px mx-1" style={{ background: i < current ? "var(--teal)" : "var(--hairline)" }} />
)}
</div>
))}
</div>
);
}
function FormError({ message }: { message: string }) {
return (
<p className="text-sm font-semibold text-[var(--rose)]">{message}</p>
);
}