skip to Main Content

I am using bycrypt for my login process and it doesn’t seem to be working. I have already stored the previously hashed password in the database so I am retrieving it from the database and then comparing it with the password but it is like my bycrpt.compare isn’t even being activated in my code. I have verified that my password and the hashed password are there, and they do work but still nothing.

Here is my code:

app.post("/login", (req, res) => {
  const username = req.body.username;
  const password = req.body.password;

  const responseArr = [];
  let passwordStatus = "";

  db.query(
    "SELECT password FROM users WHERE username = $1",
    )
    .then((res) =>  {
      
        const hash = res.rows[0].password;
        console.log("hash", hash);
        console.log("password", password);



        bcrypt.compare(hash, password, function(err, result) {
          console.log("in func", res.rows[0].password)
          console.log("in func", password)
        
              if(result){
                  console.log("login success");
                  console.log(result);

                  passwordStatus = true;
                  

              }else{
                  console.log('login failed');
                  passwordStatus = false;
              }

              if (err) {
                console.log("err", err)
              }
          })
   
        console.log("passwordStatus", passwordStatus);
        return passwordStatus;
      })
    .then((result) => {
      if (result === true) {
        const userResponse = db.query(
          "SELECT user_id FROM users WHERE username = $1;",
          
        );
        return userResponse;
      } else {
        res.status(404);
        console.log("result was false")
      }
    })
    .then((response) => {
      responseArr.push(response.rows);
      res.status(200).send(responseArr);
    })
    .catch((error) => {
      console.log("error:", error);
      if (error) {
        throw error;
      }
    });
});```

2

Answers


  1. Funtions params need to be in following manner :-

    // Load hash from your password DB.
    
    bcrypt.compare(myPlaintextPassword, hash, function(err, result) {
    // result == true
    });
    

    Replace your params order.

    Login or Signup to reply.
  2. I am using bycrypt version 5.1.1 for my login process I have already stored the previously hashed password in the database so I am retrieving it from the database and then comparing it with the password but it is like my bycrpt.compareSync() I have verified that my password and the hashed password verified and working

    Here is my code:

    let payloadpassword = 'yourpassword'
    
    
    let haspassword= 'your hash password'
    
    let comparePassword =  bycrpt.compareSync(payloadpassword , haspassword);
    
    
    if(!comparePassword)
     throw new Error()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search