2
function validateLogin(username,passwordPlaintext) {
    db.collection('users').findOne({"username": username}, (function(err, user) {
        return bcrypt.compareSync(passwordPlaintext, user.passwordHash);
    });
}

This is my current code. It receives a username and plaintext password and then hashes the password and compares it to the database. Using console.log, I've verified that everything works, but I can't figure out how to return the boolean that bcrypt produces from validateLogin().

Ezekiel
  • 2,383
  • 3
  • 19
  • 30

1 Answers1

4

You should pass in a "callback" function to be called when validateLogin is ready to return a result. The common pattern in node, is that the first argument of the callback is an error (if one happened), and the second argument is the result:

function validateLogin(username,passwordPlaintext,callback) {
    db.collection('users').findOne({"username": username}, (function(err, user) {
        if( err ) {
          return callback( err );
        }
        return callback( null, bcrypt.compareSync(passwordPlaintext, user.passwordHash ) );
    });
}
go-oleg
  • 19,272
  • 3
  • 43
  • 44