dcodeIO / bcrypt.js

Optimized bcrypt in plain JavaScript with zero dependencies.
Other
3.51k stars 267 forks source link

Bcrypt.compare always returns false. #89

Closed SamiHK closed 6 years ago

SamiHK commented 6 years ago

Thats my Register User function.

registerUser: function(User, callback) { var saltRounds =10; bcrypt.hash(User.Password, saltRounds, function(err, hash){ if(err){ throw err; } else{ return db.query( "Insert into users (FirstName, LastName, Email, Password, Phone, Pin, Type, ImageUrl, Description) values(?,?,?,?,?,?,?,?,?)", [User.FirstName, User.LastName, User.Email, hash , User.Phone, User.Pin, User.Type,User.ImageUrl, User.Description], callback ); } }); }

And That is Login Function. loginUser: function(User, callback){ var hash; db.query( "select * from users where Email=?", [User.Email], function(err, results, fields){ if(err) throw err; hash=results[0]["Password"]; bcrypt.compare(User.Password, hash, function(err, res) { if(res){ console.log('true'); return db.query( "select * from users where Email=?&&Password=?", [User.Email, hash], callback ); }else{ console.log('false'); } }); } ); } I have checked datatypes of both myPassword and hash are string.

SamiHK commented 6 years ago

I guess the Issue is From database:

-- I have stores password in varchar(50). and minimum lenght required by bcrypt is 60.

let me confirm then I'll close this issue with solution.

SamiHK commented 6 years ago

Solution : mysql table datatype of password should have a lenght of 60 or greater that 60. And then it works fine.

Ruffio commented 6 years ago

Nice :-)

Then please close this issue ;-)

el0911 commented 5 years ago

this worked for me thanks