-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforgot-password.js
120 lines (108 loc) · 3.16 KB
/
forgot-password.js
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
import { useState, useContext, useEffect } from "react";
import axios from "axios";
import { toast } from "react-toastify";
import { SyncOutlined } from "@ant-design/icons";
import Link from "next/link";
import { Context } from "../context";
import { useRouter } from "next/router";
const ForgotPassword = () => {
// state
const [email, setEmail] = useState("");
const [success, setSuccess] = useState(false);
const [code, setCode] = useState("");
const [newPassword, setNewPassword] = useState("");
const [loading, setLoading] = useState(false);
// context
const {
state: { user },
} = useContext(Context);
// router
const router = useRouter();
// redirect if user is logged in
useEffect(() => {
if (user !== null) router.push("/");
}, [user]);
const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);
try {
const { data } = await axios.post("http://localhost:8000/api/forgot-password", { email });
setSuccess(true);
toast("Check your email for the secret code");
setLoading(false);
} catch (err) {
setLoading(false);
toast(err.response.data);
}
};
const handleResetPassword = async (e) => {
e.preventDefault();
// console.log(email, code, newPassword);
// return;
try {
setLoading(true);
const { data } = await axios.post("http://localhost:8000/api/reset-password", {
email,
code,
newPassword,
});
setEmail("");
setCode("");
setNewPassword("");
setLoading(false);
toast("Great! Now you can login with your new password");
router.push("/login");
} catch (err) {
setLoading(false);
toast(err.response.data);
}
};
return (
<>
<h1 className="pt-5 text-center">
Forgot Password
</h1>
<div className="container col-md-4 offset-md-4 pb-5">
<form onSubmit={success ? handleResetPassword : handleSubmit}>
<input
type="email"
className="form-control mb-4 p-4"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Enter email"
required
/>
{success && (
<>
<input
type="text"
className="form-control mb-4 p-4"
value={code}
onChange={(e) => setCode(e.target.value)}
placeholder="Enter secret code"
required
/>
<input
type="password"
className="form-control mb-4 p-4"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
placeholder="New Password"
required
/>
<h6 className="text-primary">Also Check Spam For email </h6>
</>
)}
<Button
type="primary"
className="btn btn-primary btn-block p-2"
disabled={loading || !email}
>
{loading ? <SyncOutlined spin /> : "Submit"}
</Button>
</form>
</div>
</>
);
};
export default ForgotPassword;