ghulamabbas2/shopit

ErrorHandler: Password reset token is invalid or has been expired

SaleDjerfi opened this issue · 0 comments

Please help!

After trying to reset the password I have this error

ErrorHandler: Password reset token is invalid or has been expired
at C:\Users\Sale\Desktop\ShopIT\backend\controllers\authController.js:115:7
at processTicksAndRejections (node:internal/process/task_queues:94:5) {
statusCode: 400
}

here is code

// Reset Password => /api/v1/password/reset/:token
exports.resetPassword = catchAsyncErrors(async (req, res, next) => {
// Hash URL token
const resetPasswordToken = crypto
.createHash('sha256')
.update(req.params.token)
.digest('hex');

const user = await User.findOne({
resetPasswordToken,
resetPasswordExpire: { $gt: Date.now() },
});

if (!user) {
return next(
new ErrorHandler(
'Password reset token is invalid or has been expired',
400
)
);
}

if (req.body.password !== req.body.confirmPassword) {
return next(new ErrorHandler('Password does not match', 400));
}

// Setup new password
user.password = req.body.password;

user.resetPasswordToken = undefined;
user.resetPasswordExpire = undefined;

await user.save();

sendToken(user, 200, res);
});