-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathForgotPassword.tsx
More file actions
114 lines (106 loc) · 3.42 KB
/
ForgotPassword.tsx
File metadata and controls
114 lines (106 loc) · 3.42 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
import { useState, type FormEvent } from "react";
import { Link } from "react-router-dom";
import { authClient } from "@/lib/auth-client";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card";
export function ForgotPassword() {
const [email, setEmail] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const [success, setSuccess] = useState(false);
async function handleSubmit(e: FormEvent) {
e.preventDefault();
setError("");
setLoading(true);
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result = await (authClient as any).forgetPassword({
email,
redirectTo: "/reset-password",
});
if (result.error) {
setError(result.error.message || "Failed to send reset email.");
} else {
setSuccess(true);
}
} catch {
setError("An unexpected error occurred. Please try again.");
} finally {
setLoading(false);
}
}
if (success) {
return (
<div className="flex min-h-screen items-center justify-center px-4">
<Card className="w-full max-w-sm">
<CardHeader className="text-center">
<CardTitle className="text-2xl">Check Your Email</CardTitle>
<CardDescription>
If an account exists for <strong>{email}</strong>, we sent a
password reset link. Please check your inbox.
</CardDescription>
</CardHeader>
<CardFooter className="justify-center">
<Link
to="/login"
className="text-sm text-foreground underline underline-offset-4 hover:text-foreground/80"
>
Back to sign in
</Link>
</CardFooter>
</Card>
</div>
);
}
return (
<div className="flex min-h-screen items-center justify-center px-4">
<Card className="w-full max-w-sm">
<CardHeader className="text-center">
<CardTitle className="text-2xl">Forgot Password</CardTitle>
<CardDescription>
Enter your email and we'll send you a reset link
</CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit} className="flex flex-col gap-4">
<div className="flex flex-col gap-2">
<Label htmlFor="email">Email</Label>
<Input
id="email"
type="email"
placeholder="you@example.com"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
autoComplete="email"
/>
</div>
{error && (
<p className="text-sm text-destructive">{error}</p>
)}
<Button type="submit" className="w-full" disabled={loading}>
{loading ? "Sending..." : "Send Reset Link"}
</Button>
</form>
</CardContent>
<CardFooter className="justify-center">
<Link
to="/login"
className="text-sm text-muted-foreground hover:text-foreground"
>
Back to sign in
</Link>
</CardFooter>
</Card>
</div>
);
}