forked from codechefPesuecc/CodeChef-PESUECC-Chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForgotForm.tsx
More file actions
113 lines (108 loc) · 3.69 KB
/
Copy pathForgotForm.tsx
File metadata and controls
113 lines (108 loc) · 3.69 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
"use client";
import { useState } from "react";
import Link from "next/link";
import MechaPanel from "@/components/cp-arena/MechaPanel";
const inputCls = "mecha-input";
export default function ForgotForm() {
const [email, setEmail] = useState("");
const [error, setError] = useState<string | null>(null);
const [sent, setSent] = useState(false);
const [devLink, setDevLink] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
const submit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setError(null);
try {
const res = await fetch("/api/auth/forgot", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email }),
});
const data = await res.json();
if (!data.ok) {
setError(data.error ?? "Something went wrong.");
setLoading(false);
return;
}
setSent(true);
if (data.devLink) setDevLink(data.devLink);
} 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">
<h1 className="font-display text-2xl font-bold text-chocolate">
Reset your password
</h1>
{sent ? (
<>
<p className="mt-2 text-sm text-charcoal/70">
If an account exists for that email, we've sent a reset link.
It's valid for 30 minutes.
</p>
{devLink && (
<p className="mt-4 break-all rounded-lg border border-dashed border-bronze/40 bg-bronze/5 px-3 py-2 text-xs text-charcoal/70">
Dev mode — reset link:{" "}
<Link href={devLink.replace(/^https?:\/\/[^/]+/, "")} className="font-mono font-semibold text-bronze hover:underline">
{devLink}
</Link>
</p>
)}
<Link
href="/login"
className="mt-5 inline-block text-sm font-semibold text-bronze hover:underline"
>
← Back to log in
</Link>
</>
) : (
<>
<p className="mt-1 text-sm text-charcoal/60">
Enter your email and we'll send you a link to set a new
password.
</p>
<form onSubmit={submit} className="mt-6 space-y-4">
<div>
<label
htmlFor="email"
className="mb-1 block text-xs font-medium text-charcoal/70"
>
Email
</label>
<input
id="email"
type="email"
className={inputCls}
value={email}
onChange={(e) => setEmail(e.target.value)}
autoComplete="email"
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 ? "Sending…" : "Send reset link"}
</button>
</form>
<p className="mt-5 text-center text-sm text-charcoal/60">
Remembered it?{" "}
<Link href="/login" className="font-semibold text-bronze hover:underline">
Log in
</Link>
</p>
</>
)}
</MechaPanel>
</div>
);
}