meabhisingh / mernProjectEcommerce

This is MERN Stack Ecommerce Project Made to Teach MERN Stack on YouTube
1.15k stars 828 forks source link

Forgot Password #65

Closed Kanai2003 closed 1 year ago

Kanai2003 commented 1 year ago

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 1 year ago

after analyzing frontend, I think, the problem lies in Forgot password component and way it passes data to action creator

deep0133 commented 1 year ago

@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 1 year ago

Thank you! @aayush6200 @deep0133