Kabya Singh
Kabya Singh

Reputation: 27

TypeError: user.verifyPassword is not a function

My model looks like this, but when I try use verifyPassword, it says TypeError: user.verifyPassword is not a function

var passport = require('passport');
var BasicStrategy = require('passport-http').BasicStrategy;
var User = require('../models/user');

passport.use(new BasicStrategy(
      function(username, password, callback) {
        User.findOne({ username: username }, function (err, user) {
          if (err) { return callback(err); }

          // No user found with that username
          if (!user) { return callback(null, false); }

          // Make sure the password is correct
          // Error comind at this point
          user.verifyPassword(password, function(err, isMatch) {
            if (err) { return callback(err); }

            // Password did not match
            if (!isMatch) { return callback(null, false); }

            // Success scenario
            return callback(null, user);
          });
        });
      }
));

Upvotes: 3

Views: 5096

Answers (4)

Mohhamad Hasham
Mohhamad Hasham

Reputation: 2032

I know i am late but you can do something like this,

passport.use(
    'local',
    new LocalStrategy(function(username, password, done) {
        
        User.findOne({ username: username }, function(err, user) {
            if (err) {
                return done(err);
            }
            if (!user) {
                return done(null, false);
            }
            if (user.password != password) {
                return done(null, false);
            }
            return done(null, user);
        });
    })
);

Reference : https://github.com/sikandar114/node-auth-pass-local/blob/master/config/passport.js

Upvotes: 2

theParadox42
theParadox42

Reputation: 65

Try using passport.use(new LocalStrategy(User.authenticate()));

Upvotes: 0

Edison D'souza
Edison D'souza

Reputation: 4640

Since the data is sent by post method, express is not able to parse data because body-parser middleware has been omitted from express due to some issues. Hence you must add it manually.Import body-parser:

npm install body-parser

Add these lines to your code.

    app.use(bodyParser.urlencoded({ extended: false })); 
    app.use(passport.initialize());

Also define these 2 functions.

    passport.serializeUser(function(user, done) {
        done(null, user); 
    });

    passport.deserializeUser(function(user, done) {
        done(null, user); 
    });

Upvotes: 0

Denis Tambovchanin
Denis Tambovchanin

Reputation: 556

Try to use User.verifyPassword function

But for more correct answer show your User model

You should have in your User schema something like:

// I am use bctypt, but you need your comparer function
userSchema.methods.verifyPassword = function(password, callback) {
  callback(err, bcrypt.compareSync(password, this.password));
};

Upvotes: 2

Related Questions