title: Node bCrypt.compareSync
body: <p>I am creating a user login. I am able to have the user sign up and when the user sings up his password is encrypted before it is saved in the database.</p>
  
  <p>When that same user tries to log in, I am getting an "invalid password". </p>
  
  <p>This is because it is comparing the user input to an encrypted password in the database. Example if password is 1234, then in database it is saved as "$2a$104$0301". When the user tries to log in, the user input which is "1234" is compared to "2a$104$0301". How would I fix?</p>
  
  <p>Here is my code for login: </p>
  
  <pre><code>var LocalStrategy = require('passport-local').Strategy;
  var User = require('../Models/users.js');
  var bcrypt = require('bcrypt-nodejs');
  
  module.exports = function(passport){
  passport.use('login', new LocalStrategy({
       passReqToCallback : true
   },
      function(req, username, password, done){
          User.findOne({'username' : username},
          function(err, user){
              if(err)
                  return done(err);
              if(!user){
                  console.log('User Not Found with username: '+username);
                  return done(null, false,
                      req.flash('message', 'User Not Found.'));
              }
              if (!isValidPassword(user, password)){
                  console.log('Invalid Password');
                  return done (null, false,
                      req.flash('message', 'Invalid Password'));
              }
              return done(null, user);
          }
      );
  })
  );
  var isValidPassword = function(user, password){
      var result = bcrypt.compareSync(password, user.password);
      if (result) {
       console.log("Password correct");
      } else {
      console.log("Password wrong");
      }
      return result;
  
   }
  }
  </code></pre>