Skip to content

bcrypt compare is always returning false #63

@anantnema

Description

@anantnema

I am storing the hashed password in the mongodb and then trying to compare the password in db with the user input password but this function is always returning false no matter whatever be the case.

  var mongoose = require('mongoose');
  var bcrypt = require('bcryptjs');
  var express = require('express');
  var router = express.Router();
  var passport = require('passport');
  var LocalStrategy = require('passport-local').Strategy;

  module.exports.comparePassword = function(candidatePassword, hash, callback){
   // Load hash from your password DB.

   bcrypt.compare(candidatePassword, hash, function(err, isMatch) {
    // res === true
       if(err) throw err;
       else return callback(null, isMatch);
   });
   }

getUserByUsername is working fine but the compare password is returning always "Invalid password"

   passport.use(new LocalStrategy(
      function(username, password, done) {
      User.getUserByUsername(username, function(err, user){
         if(err) throw err;
         if(!user){
         return done(null, false, {message: 'Unknown User'});
      }

      User.comparePassword(password, user.password, function(err, isMatch){
         if(err) throw err;
         //(this isMatch is always false)
         if(!isMatch) {

             return done(null, false, {message: 'Invalid password'});
          }
        return done(null, user);
        });
      });
    }));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions