-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathpage.tsx
More file actions
95 lines (86 loc) · 2.76 KB
/
Copy pathpage.tsx
File metadata and controls
95 lines (86 loc) · 2.76 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
"use client";
import { useState, Suspense } from "react";
import { useSearchParams, useRouter } from "next/navigation";
import Link from "next/link";
function ResetPasswordForm() {
const searchParams = useSearchParams();
const router = useRouter();
const token = searchParams.get("token");
const [password, setPassword] = useState("");
const [confirm, setConfirm] = useState("");
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [success, setSuccess] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (password !== confirm) {
setError("Passwords do not match");
return;
}
setLoading(true);
setError("");
const res = await fetch("/api/auth/reset-password", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token, password }),
});
const data = await res.json();
setLoading(false);
if (!res.ok) {
setError(data.error || "Something went wrong");
} else {
setSuccess(true);
setTimeout(() => router.push("/login"), 2000);
}
};
if (success) {
return (
<div className="text-center space-y-4">
<h1 className="text-2xl font-bold">Password Reset!</h1>
<p className="text-muted-foreground">Redirecting you to login...</p>
</div>
);
}
return (
<div className="w-full max-w-md space-y-6 p-6">
<h1 className="text-2xl font-bold">Reset Password</h1>
<p className="text-muted-foreground">Enter your new password below.</p>
<form onSubmit={handleSubmit} className="space-y-4">
<input
type="password"
placeholder="New password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
className="w-full border rounded px-3 py-2"
/>
<input
type="password"
placeholder="Confirm new password"
value={confirm}
onChange={(e) => setConfirm(e.target.value)}
required
className="w-full border rounded px-3 py-2"
/>
{error && <p className="text-red-500 text-sm">{error}</p>}
<button
type="submit"
disabled={loading}
className="w-full bg-black text-white py-2 rounded hover:bg-gray-800"
>
{loading ? "Resetting..." : "Reset Password"}
</button>
</form>
<Link href="/login" className="text-sm hover:underline">Back to login</Link>
</div>
);
}
export default function ResetPasswordPage() {
return (
<div className="flex min-h-screen items-center justify-center">
<Suspense fallback={<div>Loading...</div>}>
<ResetPasswordForm />
</Suspense>
</div>
);
}