forked from codechefPesuecc/CodeChef-PESUECC-Chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerifyForm.tsx
More file actions
182 lines (173 loc) · 5.96 KB
/
Copy pathVerifyForm.tsx
File metadata and controls
182 lines (173 loc) · 5.96 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
"use client";
import { useCallback, useEffect, useRef, useState } from "react";
import Link from "next/link";
import { useUser } from "@/components/auth/useUser";
import MechaPanel from "@/components/cp-arena/MechaPanel";
export default function VerifyForm() {
const user = useUser();
const [code, setCode] = useState("");
const [error, setError] = useState<string | null>(null);
const [info, setInfo] = useState<string | null>(null);
const [devCode, setDevCode] = useState<string | null>(null);
const [verifying, setVerifying] = useState(false);
const [sending, setSending] = useState(false);
const [done, setDone] = useState(false);
const sentRef = useRef(false);
const send = useCallback(async (initial: boolean) => {
setSending(true);
setError(null);
try {
const res = await fetch("/api/auth/resend", { method: "POST" });
const data = await res.json();
if (!res.ok || !data.ok) {
// On the initial auto-send, a cooldown just means a code is already out.
if (initial && res.status === 429) {
setInfo("A code is on its way — check your inbox.");
} else {
setError(data.error ?? "Couldn't send a code.");
}
return;
}
setInfo("We sent a 6-digit code to your email.");
if (data.devCode) setDevCode(data.devCode);
} catch {
setError("Couldn't reach the server.");
} finally {
setSending(false);
}
}, []);
// Send the first code once, as soon as we know the user is unverified.
useEffect(() => {
if (!user || user.emailVerified || sentRef.current) return;
sentRef.current = true;
void send(true);
}, [user, send]);
const verify = async (e: React.FormEvent) => {
e.preventDefault();
if (!/^\d{6}$/.test(code)) {
setError("Enter the 6-digit code.");
return;
}
setVerifying(true);
setError(null);
try {
const res = await fetch("/api/auth/verify", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ code }),
});
const data = await res.json();
if (!res.ok || !data.ok) {
setError(data.error ?? "Verification failed.");
setVerifying(false);
return;
}
setDone(true);
setTimeout(() => {
window.location.href = "/cp-arena";
}, 1200);
} catch {
setError("Couldn't reach the server.");
setVerifying(false);
}
};
return (
<div className="mx-auto flex max-w-md flex-col justify-center px-6 py-12">
<MechaPanel bodyClassName="p-8">
{user === undefined ? (
<p className="text-sm text-charcoal/60">Loading…</p>
) : user === null ? (
<>
<h1 className="font-display text-2xl font-bold text-chocolate">
Verify your email
</h1>
<p className="mt-2 text-sm text-charcoal/70">
Please log in first to verify your email.
</p>
<Link
href="/login"
className="mecha-btn mecha-btn--solid mt-5 w-full"
>
Log in
</Link>
</>
) : user.emailVerified ? (
<>
<h1 className="font-display text-2xl font-bold text-chocolate">
Email verified
</h1>
<p className="mt-2 text-sm text-charcoal/70">
Your email is verified — you're all set.
</p>
<Link
href="/cp-arena"
className="mecha-btn mecha-btn--solid mt-5 w-full"
>
Go to the Arena
</Link>
</>
) : done ? (
<>
<h1 className="font-display text-2xl font-bold text-emerald-600 dark:text-emerald-400">
Verified!
</h1>
<p className="mt-2 text-sm text-charcoal/70">
Taking you to the Arena…
</p>
</>
) : (
<>
<h1 className="font-display text-2xl font-bold text-chocolate">
Verify your email
</h1>
<p className="mt-1 text-sm text-charcoal/60">
Enter the 6-digit code sent to{" "}
<span className="font-semibold text-brown">{user.email}</span>.
</p>
{devCode && (
<p className="mt-3 rounded-lg border border-dashed border-bronze/40 bg-bronze/5 px-3 py-2 text-xs text-charcoal/70">
Dev mode — your code is{" "}
<span className="font-mono font-bold text-bronze">{devCode}</span>
</p>
)}
<form onSubmit={verify} className="mt-6 space-y-4">
<input
inputMode="numeric"
autoComplete="one-time-code"
maxLength={6}
value={code}
onChange={(e) =>
setCode(e.target.value.replace(/\D/g, "").slice(0, 6))
}
placeholder="000000"
className="mecha-input py-3 text-center font-mono text-2xl tracking-[0.4em]"
required
/>
{error && (
<p className="text-sm text-red-600 dark:text-red-400">{error}</p>
)}
{info && !error && (
<p className="text-sm text-charcoal/60">{info}</p>
)}
<button
type="submit"
disabled={verifying || code.length !== 6}
className="mecha-btn mecha-btn--solid w-full"
>
{verifying ? "Verifying…" : "Verify"}
</button>
</form>
<button
type="button"
onClick={() => send(false)}
disabled={sending}
className="mt-4 text-sm font-medium text-bronze hover:underline disabled:opacity-60"
>
{sending ? "Sending…" : "Resend code"}
</button>
</>
)}
</MechaPanel>
</div>
);
}