forked from codechefPesuecc/CodeChef-PESUECC-Chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResetForm.tsx
More file actions
153 lines (147 loc) · 4.89 KB
/
Copy pathResetForm.tsx
File metadata and controls
153 lines (147 loc) · 4.89 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
"use client";
import { useEffect, useState } from "react";
import Link from "next/link";
import MechaPanel from "@/components/cp-arena/MechaPanel";
const inputCls = "mecha-input";
export default function ResetForm() {
// undefined = reading the URL, "" = no token, string = the reset token
const [token, setToken] = useState<string | null | undefined>(undefined);
const [password, setPassword] = useState("");
const [confirm, setConfirm] = useState("");
const [error, setError] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
const [done, setDone] = useState(false);
// Read the token after mount (defer the state set so it isn't set in the
// effect body, and so there's no server/client hydration mismatch).
useEffect(() => {
const id = setTimeout(() => {
const t = new URLSearchParams(window.location.search).get("token");
setToken(t ?? "");
}, 0);
return () => clearTimeout(id);
}, []);
const submit = async (e: React.FormEvent) => {
e.preventDefault();
if (password !== confirm) {
setError("Passwords don't match.");
return;
}
if (password.length < 8) {
setError("Password must be at least 8 characters.");
return;
}
setLoading(true);
setError(null);
try {
const res = await fetch("/api/auth/reset", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token, password }),
});
const data = await res.json();
if (!data.ok) {
setError(data.error ?? "Reset failed.");
setLoading(false);
return;
}
setDone(true);
setTimeout(() => {
window.location.href = "/login";
}, 1400);
} catch {
setError("Couldn't reach the server.");
setLoading(false);
}
};
return (
<div className="mx-auto flex min-h-[70vh] max-w-md flex-col justify-center px-6 py-16">
<MechaPanel bodyClassName="p-8">
{token === undefined ? (
<p className="text-sm text-charcoal/60">Loading…</p>
) : token === "" ? (
<>
<h1 className="font-display text-2xl font-bold text-chocolate">
Invalid reset link
</h1>
<p className="mt-2 text-sm text-charcoal/70">
This link is missing its token. Request a fresh one.
</p>
<Link
href="/forgot"
className="mecha-btn mecha-btn--solid mt-5 w-full"
>
Request a new link
</Link>
</>
) : done ? (
<>
<h1 className="font-display text-2xl font-bold text-emerald-600 dark:text-emerald-400">
Password updated
</h1>
<p className="mt-2 text-sm text-charcoal/70">
Taking you to the log-in page…
</p>
</>
) : (
<>
<h1 className="font-display text-2xl font-bold text-chocolate">
Set a new password
</h1>
<p className="mt-1 text-sm text-charcoal/60">
Choose a new password for your account.
</p>
<form onSubmit={submit} className="mt-6 space-y-4">
<div>
<label
htmlFor="password"
className="mb-1 block text-xs font-medium text-charcoal/70"
>
New password
<span className="ml-1 font-normal text-charcoal/45">
· at least 8 characters
</span>
</label>
<input
id="password"
type="password"
className={inputCls}
value={password}
onChange={(e) => setPassword(e.target.value)}
autoComplete="new-password"
required
/>
</div>
<div>
<label
htmlFor="confirm"
className="mb-1 block text-xs font-medium text-charcoal/70"
>
Confirm password
</label>
<input
id="confirm"
type="password"
className={inputCls}
value={confirm}
onChange={(e) => setConfirm(e.target.value)}
autoComplete="new-password"
required
/>
</div>
{error && (
<p className="text-sm text-red-600 dark:text-red-400">{error}</p>
)}
<button
type="submit"
disabled={loading}
className="mecha-btn mecha-btn--solid w-full"
>
{loading ? "Updating…" : "Update password"}
</button>
</form>
</>
)}
</MechaPanel>
</div>
);
}