Passport fails authentication with "Wrong password" before actually calling my passwordMatch function Passport fails authentication with "Wrong password" before actually calling my passwordMatch function mongoose mongoose

Passport fails authentication with "Wrong password" before actually calling my passwordMatch function


In the "passMatch" function, I query for the user again (which is just inefficient), but since this operation was asynch, it was being skipped to the "return false" statement after, and in the passport authentication config process, it recieved that false, causing authentication to fail, but the "log" to be returned after cause it took longer.

How I fixed it

I passed in the user object that passport already queried instead of the username into passMatch, then had two operations to check if the hash was the same and returned that, and now it works.

The new code

module.exports.passMatch = (user, password) => {    var hash = crypto.pbkdf2Sync(password, user.salt, 1000, 64, 'sha512').toString('hex');    return user.hash == hash;};

Also the necessary change in the passport config to pass in the user instead of the username as the first param to that function.