Forgot Password
Closed this issue · 3 comments
Kanai2003 commented
UserControler/forgot_password is not working. always showing User not found. Code is exactly same.
// Forgot password --- this function is not working, always showing "User not found" message
exports.forgotPassword = catchAsyncErrors(async (req, res, next) => {
const user = await User.findOne({ email: req.body.email });
if (!user) {
return next(new ErrorHandler("User not found", 404));
}
// get resetPasswordToken
const resetToken = user.getResetPasswordToken();
await user.save({validateBeforeSave: false});
const resetPasswordUrl = `${req.protocol}//${req.get("host")}/api/v1/password/reset/${resetToken}`
const message = `Your password reset token is : \n\n ${resetPasswordUrl} \n\nIf you have not requested this email then, please ignore it `;
try{
await sendEmail({
email: user.email,
subject: `Ecommerce password Recovery`,
message,
})
res.status(200).json({
success:true,
message: `Email sent to ${user.email} successfully`,
});
}catch (error) {
user.resetPasswordToken = undefined;
user.resetPasswordExpire = undefined;
await user.save({validateBeforeSave: false});
return next(new ErrorHandler(error.message, 500));
}
})
aayush6200 commented
after analyzing frontend, I think, the problem lies in Forgot password component and way it passes data to action creator
deep0133 commented
@KanailalManna console two things : 1) [ req.body.email ] and 2) [ user ] . if email found and user not found then check the email in database ( may be email not exist in db ) . Try it
Kanai2003 commented
Thank you! @aayush6200 @deep0133