diff --git a/client/src/component/ForgotPassword.jsx b/client/src/component/ForgotPassword.jsx index d16707e..5911967 100644 --- a/client/src/component/ForgotPassword.jsx +++ b/client/src/component/ForgotPassword.jsx @@ -10,7 +10,6 @@ const ForgotPassword = ({ showAlert, mode }) => { const [forgotEmail, setForgotEmail] = useState(""); const [loading, setLoading] = useState(false); const navigate = useNavigate(); - const handleForgotPassword = async () => { setLoading(true); try { @@ -21,14 +20,15 @@ const ForgotPassword = ({ showAlert, mode }) => { }, body: JSON.stringify({ email: forgotEmail }), }); - + const data = await response.json(); + if (response.ok) { toast.success(data.message || "Reset email sent successfully!"); setForgotEmail(""); navigate("/login"); } else { - showAlert(data.message || "Failed to send reset email", "danger"); + showAlert(data.error || "Failed to send reset email", "danger"); } } catch (error) { console.error("Error during password reset:", error); @@ -37,6 +37,7 @@ const ForgotPassword = ({ showAlert, mode }) => { setLoading(false); } }; + return (
diff --git a/server/Controllers/auth.js b/server/Controllers/auth.js index 0c5eb6e..1e084cb 100644 --- a/server/Controllers/auth.js +++ b/server/Controllers/auth.js @@ -97,10 +97,22 @@ const verifyToken = async (req, res) => { } }; -async function ResetPasswordByEmail(req, resp) { - const VITE_CLIENT_PORT = process.env.VITE_CLIENT_PORT || "https://bitbox-in.netlify.app"; +async function ResetPasswordByEmail(req, res) { + const VITE_CLIENT_PORT = process.env.VITE_CLIENT_PORT || "https://bitbox-in.netlify.app"; const { email } = req.body; + + // Check if email is provided + if (!email) { + return res.status(400).json({ error: 'Email is required' }); + } + + const user = await User.findOne({ email }); + + if (!user) { + return res.status(404).json({ error: 'User does not exist' }); + } + const transporter = nodemailer.createTransport({ service: "gmail", auth: { @@ -112,24 +124,23 @@ async function ResetPasswordByEmail(req, resp) { const mailOptions = { from: process.env.EMAIL_USER, to: email, - subject: "Reset Your password on BitBox", + subject: "Reset Your Password on BitBox", html: ` -

Reset your password using the link below:

- to reset your password - `, +

Reset your password using the link below:

+ to reset your password + `, }; transporter.sendMail(mailOptions, (error, info) => { if (error) { - console.log("Error sending email: " + error); - resp.status(500).send("Error sending email"); + return res.status(500).json({ error: "Error sending email" }); } else { - console.log("Email sent: " + info.response); - resp.status(200).send({ message: "email sent successfully" }); + return res.status(200).json({ message: "Email sent successfully" }); } }); } + const forgetpassword = async (req, res) => { try { const { email, password } = req.body;