TommyD
TommyD

Reputation: 1043

Hashed pass, compare the result

I have a postgres datbase with a node backend.

  let hash = bcrypt.hashSync("mypass", 10);

  try {
    const queryString = `
        SELECT * 
        FROM users where username= 'admin' and password='${hash}'
  `;

This returns 0 rows.

Do I have to return the password and then run the compare?

 try {
        const queryString = `
            SELECT username, password
            FROM users where username= 'admin'
      `;

       bcrypt.compare(RETURNED PASSWORD, hash, function(err, res) {
          if(res) {
           // Passwords match
          } else {
           // Passwords don't match
          } 
        });

Upvotes: 0

Views: 42

Answers (1)

Osama
Osama

Reputation: 106

Yes since the bcrypt algorithm creates different hash for same input, so you have to return hashed password and then compare it.

Upvotes: 2

Related Questions