Everything in my passport-local strategy seems to be working, except the verifyCallback, where the truthiness evaluation of the validatesPassword
function returns true regardless. The problem could stem from the fact that validatesPassword
is an asynchronous function and the Promise {<pending>}
output while the function works out of order isn't false:
from passwordUtils file:
async function validatesPassword(passwordInput, dbHash) {
let comparedInput = await bcrypt.compare(passwordInput, dbHash)
//console.log("this is whats returned from compared Input " + comparedInput)
return comparedInput;
}
The console.log call in validatesPassword
normally prints after the conditional statement in the passport verifyCallback() function in my passport file is called:
User.findOne(email)
.then((dbResponse) => {
//console.log(dbResponse[0][0]);
let user = dbResponse[0][0];
if (!user) { return done(null, false); }
//no error, but also no user
const isValid = validatesPassword(password, user.hash);
//const isValid = false;
if (isValid) {
return done(null, user);
} else {
return done(null, false);
}
})
.catch((err) => {
console.log(err);
done(err);
});
}
const strategy = new LocalStrategy({ usernameField: 'email', passReqToCallback: true }, verifyCallback);
passport.use(strategy)
...
As seen above, the only way I can derive false from the aforementioned conditional is explicitly setting isValid
to false. What would be the best way to get the conditional to wait for the password compare (validatesPassword
) function and evaluate its returned boolean function? Should I promisify validatesPassword
and add the conditional inside (I've tried implementing this on my own to no avail) of that function and pass all of that to the verifyCallback
function in my passport file?