Skip to content

Commit 21f7242

Browse files
authored
Merge pull request #337 from VinayLodhi1712/forgotpasswordmailcheck
Forgot password mail check exist or not #329 done
2 parents cdc5c09 + de68473 commit 21f7242

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

Diff for: client/src/component/ForgotPassword.jsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const ForgotPassword = ({ showAlert, mode }) => {
1010
const [forgotEmail, setForgotEmail] = useState("");
1111
const [loading, setLoading] = useState(false);
1212
const navigate = useNavigate();
13-
1413
const handleForgotPassword = async () => {
1514
setLoading(true);
1615
try {
@@ -21,14 +20,15 @@ const ForgotPassword = ({ showAlert, mode }) => {
2120
},
2221
body: JSON.stringify({ email: forgotEmail }),
2322
});
24-
23+
2524
const data = await response.json();
25+
2626
if (response.ok) {
2727
toast.success(data.message || "Reset email sent successfully!");
2828
setForgotEmail("");
2929
navigate("/login");
3030
} else {
31-
showAlert(data.message || "Failed to send reset email", "danger");
31+
showAlert(data.error || "Failed to send reset email", "danger");
3232
}
3333
} catch (error) {
3434
console.error("Error during password reset:", error);
@@ -37,6 +37,7 @@ const ForgotPassword = ({ showAlert, mode }) => {
3737
setLoading(false);
3838
}
3939
};
40+
4041

4142
return (
4243
<div className="flex justify-center h-[85vh] items-center mt-[7rem]">

Diff for: server/Controllers/auth.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,22 @@ const verifyToken = async (req, res) => {
9797
}
9898
};
9999

100-
async function ResetPasswordByEmail(req, resp) {
101-
const VITE_CLIENT_PORT = process.env.VITE_CLIENT_PORT || "https://bitbox-in.netlify.app";
100+
async function ResetPasswordByEmail(req, res) {
102101

102+
const VITE_CLIENT_PORT = process.env.VITE_CLIENT_PORT || "https://bitbox-in.netlify.app";
103103
const { email } = req.body;
104+
105+
// Check if email is provided
106+
if (!email) {
107+
return res.status(400).json({ error: 'Email is required' });
108+
}
109+
110+
const user = await User.findOne({ email });
111+
112+
if (!user) {
113+
return res.status(404).json({ error: 'User does not exist' });
114+
}
115+
104116
const transporter = nodemailer.createTransport({
105117
service: "gmail",
106118
auth: {
@@ -112,24 +124,23 @@ async function ResetPasswordByEmail(req, resp) {
112124
const mailOptions = {
113125
from: process.env.EMAIL_USER,
114126
to: email,
115-
subject: "Reset Your password on BitBox",
127+
subject: "Reset Your Password on BitBox",
116128
html: `
117-
<p>Reset your password using the link below:</p>
118-
<a href="${VITE_CLIENT_PORT}/reset-password"><button>Click here</button></a> to reset your password
119-
`,
129+
<p>Reset your password using the link below:</p>
130+
<a href="${VITE_CLIENT_PORT}/reset-password"><button>Click here</button></a> to reset your password
131+
`,
120132
};
121133

122134
transporter.sendMail(mailOptions, (error, info) => {
123135
if (error) {
124-
console.log("Error sending email: " + error);
125-
resp.status(500).send("Error sending email");
136+
return res.status(500).json({ error: "Error sending email" });
126137
} else {
127-
console.log("Email sent: " + info.response);
128-
resp.status(200).send({ message: "email sent successfully" });
138+
return res.status(200).json({ message: "Email sent successfully" });
129139
}
130140
});
131141
}
132142

143+
133144
const forgetpassword = async (req, res) => {
134145
try {
135146
const { email, password } = req.body;

0 commit comments

Comments
 (0)