I encountered an issue while attempting to send a token to the user for password reset. The error message I received is as follows:
Error: there was an error sending the email, try again later
at exports.forgotPassword (C:\\Users\\Abdurehman\\Desktop\\node course\\node practice\\FinalYearProject\\controllers\\authController.js:167:7)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
I have searched extensively for a solution to this problem but have not been able to find one. Currently, I have not implemented the password resetting functionality as I first need to resolve the issue with sending the token.
The file in question is named authController.js
.
Below is the code snippet:
exports.forgotPassword = async (req, res, next) => {
const user = await User.findOne({ email: req.body.email });
if (!user) {
return next(new AppError('no user with this email', 404));
}
const resetToken = user.createPasswordResetToken();
await user.save({ validateBeforeSave: false });
const resetURL = `${req.protocol}://${req.get(
'host'
)}/api/v1/users/resetPassword/${resetToken}`;
const message = `forgot your password ? submit a PATCH request with your new password to : ${resetURL}.\n If you didn't forget your password , please ignore this email`;
try {
await sendEmail({
email: user.email,
subject: 'your password reset token (valid for 10 minutes)',
message,
});
res.status(200).json({
status: 'success',
message: 'Token sent to email',
});
} catch (err) {
user.passwordResetToken = undefined;
user.passwordResetExpires = undefined;
await user.save({ validateBeforeSave: false });
return next(
new AppError('there was an error sending the email , try again later'),
500
);
}
};
Any assistance provided would be greatly appreciated.