shaneMangudi / bcrypt-nodejs

Native implementation of bcrypt for NodeJS
Other
574 stars 69 forks source link

bcrypt compare returns false all the time #45

Closed justinjwlee closed 9 years ago

justinjwlee commented 9 years ago

Hi there,

I'm implementing user authentication using bcrypt. Bcrypt compare is returning false despite the password being correct. Interestingly, bcrypt compare works on a local db but not on a hosted remote db. (not a password string length issue) Any idea what might be the problem?

UserSchema.methods.validPassword = function (password, next) {
    var user = this;

    bcrypt.compare(password, user.password, function (err, isMatch) {
        if (err) {
            console.log('PW COMPARE ERR: Validation process failed')
            next(err)
        };

        next(null, isMatch)
    })
};
justinjwlee commented 9 years ago

Sorry it was entirely my own issue. Rectified & closed

ms88privat commented 9 years ago

i am faceing the same problem - what was your mistake? maybe its mine too...

edit: solved it, my issue was, that i had datatype CHAR instead of STRING for my password - but in console.log and my database i could not visual see any difference

justinjwlee commented 9 years ago

if you're using mongoose and using bcrypt hash in the pre-save method of mongoose in the models, make sure in your function you're only calling 'save' once. If not hash will be called more than once which will naturally fail bcrypt compare.

Again, this is my own error entirely unrelated to bcrypt

On Sun, May 24, 2015 at 11:18 AM ms88privat notifications@github.com wrote:

i am faceing the same problem - what was your mistake? maybe its mine too...

— Reply to this email directly or view it on GitHub https://github.com/shaneGirish/bcrypt-nodejs/issues/45#issuecomment-104972572 .

PsyEnmanuel commented 7 years ago

thanks you, i was having a similar mistake.