'I am trying to save the reset password back to database but request taking longer until i cancel

I am not receiving any errors but when I test my endpoint it's taking longer and no response until I terminate the request. I am sending a user an email with a token that will be used to validate if he exists in the database so that he can change his/her password but I have not succeeded for the last two days. I am frustrated now, I have never done this before.

The Middleware that sends the reset password link

 export class sendGridEmail {
  static async sendResetPasswordEmail(email, token) {
    sendGrid.setApiKey(process.env.SENDGRID_API_KEY);
    const msg = {
      to: `${email}`,
      from: `${process.env.VERIFIED_SENDER}`, // Change to your verified sender
      subject: "RESET YOUR PASSWORD",
      text: `You are receiving this email because you (or someone else) has requested the reset of a password. Follow this link ${process.env.BASE_URL}/api/resetpassword/${token}`,
    };
    return sendGrid
      .send(msg)
      .then(() => {
        console.log(`password rest link has been sent to: ${email}`);
      })
      .catch((err) => {
        console.log(err);
      });
  }
}

The Component that sends the reset password link

export const sendResetPasswordLink = asynchandler(async (req, res) => {
  const { email } = req.body;
  const user = await userModel.findOne({ email });
  if (!user) {
    res.status(404);
    res.json({ message: "account with this email was not found" });
  } else if (user) {
    const token = AuthToken(user._id);
    try {
      await sendGridEmail.sendResetPasswordEmail(user.email, token);
      res.status(200);
      res.json({
        message: `password reset link hase been sent to: ${user.email}`,
      });
    } catch (error) {
      res.status(500);
      res.json({ message: error });
    }
  } else {
    res.status(500);
    res.json({ message: "Internal Server Error" });
  }
});

The Route that tries to save the password. Am I getting it wrong by verifying the token in the params using jwt and then checking if the user exists or am I missing out something ?

export const resetPassword = asynchandler(async (req, res) => {
  const { resetToken } = req.params;
  const private_key = process.env.PRIVATE_KEY;
  const payload = jwt.verify(resetToken, private_key);
  const user = await userModel.findById(payload.id);
  console.log(payload.id);

  if (!user) {
    res.status(404);
    res.jsonp({ message: "token has expired" });
  }else if(user){
    user.password= req.body.password
    await user.save();
    await resetToken.delete();
    await sendMessage.sendPasswordResetSuccess(user.number);
    res.status(200);
    res.json({message:"password changed succesfully"});
  }else{
    res.status(500)
    res.json({message:"no token was procide"})
  }

});

The routes

    app.post('/api/resetlink', sendResetPasswordLink);
    app.put("/api/resetpassword/:resetToken", resetPassword);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source