Compare if the value of a variable is the same as the value already saved in the database

Asked

Viewed 58 times

0

Using Java Script, Nodejs, Adonisframework, Mysql, Lucid ORM

I need to check compare if the value sent by the client through the API is the same present in my database. I need to check if the variable token passed by the user in the request, is exactly the same as passwordResetToken which is a field of a table from my Mysql database

I believe it must be some function of Lucid ORM, compare whether the values are equal. Or even ride a query Builder for such confirmation.

I have not yet succeeded, even if I try in many different ways. If anyone gets this syntax or function should be used and can help me, I appreciate it now!

Yours sincerely, William Henry


 async resetPassword({ request, response }) {
    const { email, token, password } = request.body;
    

   
    //ERROR MYSQL KNEX-LUCID SINTAXE METHOD
    try {
     

      const user = await User.findBy("email", email);
      const verificationToken = await User.findBy("passwordResetToken");

      //CHECK IF EMAIL IN DB IS VALID
      if (user == null) {
        return response
          .status(400)
          .send({ error: "An existing valid E-mail must be sent" });
      }

      //CHECK IF TOKEN IS VALID-FEATURE
      if (token != verificationToken) {
        return response.status(400).send({
          error: "Your token is wrong, please try again with a valid token",
        });
      }

      if (token == verificationToken) {
        user.merge({
          //passwordResetToken: token,
          password: password,
        });

        await user.save();

        response.status(200).json({
          message: "Password changed successfully",
        });
      }
  

      user.merge({
        //passwordResetToken: token,
        password: password,
      });

      await user.save();
    } catch (err) {
      console.log(err);
      response.status(400).send({ error: "Cannot reset password, try again" });
    }

  • https://answall.com/users/103212/guillerbr

1 answer

0


RESOLVED


async resetPassword({ request, response }) {
    const { email, token, password } = request.body;
    
   
    try {
      const user = await User.findBy("email", email);
      const verificationToken = user.passwordResetToken;

      

      console.log(verificationToken);

      //CHECK IF EMAIL IN DB IS VALID
      if (user == null) {
        return response
          .status(400)
          .send({ error: "An existing valid E-mail must be sent" });
      }

      //CHECK IF TOKEN IS VALID-FEATURE
      if (token != verificationToken) {
        return response.status(400).send({
          error: "Your token is wrong, please try again with a valid token",
        });
      }

      if (token == verificationToken) {
        user.merge({
          //passwordResetToken: token,
          password: password,
        });

        await user.save();

        response.status(200).json({
          message: "Password changed successfully",
        });
      }

      user.merge({
        
        password: password,
      });

      await user.save();
    } catch (err) {
      console.log(err);
      response.status(400).send({ error: "Cannot reset password, try again" });
    }


Browser other questions tagged

You are not signed in. Login or sign up in order to post.