Rajesh Barik
Rajesh Barik

Reputation: 191

passport is throwing user.authenticate is not a function

I know this question has been asked before,but I couldn't found a proper answer. Here is the console error.

TypeError: user.authenticate is not a function
at /home/sinnedde/WebstormProjects/web-services/config/strategies/local.js:24:23

Here is local.js used to check if the username and password are correct.

var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
User = require('mongoose').model('User');

module.exports = function () {
passport.use(new LocalStrategy(function (username, password, done) {
    User.findOne({
        username: username
    }, function (err, user) {
        if (err) {
           return done(err);
        }
        if (!user) {
            return done(null, false, {
                message: 'Invalid Username or Password'
            });
        }
        if (!user.authenticate(password)) {
            return done(null, false, {
                message: 'Invalid Username or Password'
            });
        }
        return done(null, user);
    });
 }));
};

Here is the signin method in the controller.

exports.signin = function (req, res, next) {
passport.authenticate('local', function (err, user, info) {
   if (err || !user) {
       res.send(info);
   } else {
       res.json({
           status: 'true',
           message: 'Logged In'
       });
   }
 })(req, res, next);
};

I am sending a request through postman. If the username is not valid I am getting proper response, but if the password is not valid, it's throwing above error. I don't know what's wrong. Please help.

Upvotes: 1

Views: 9808

Answers (3)

vinod
vinod

Reputation: 2880

As 'Hya' already said

Your Mongoose model does not have authenticate method, but you can add it to your schema.

Example code:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;


var UserSchema = new Schema ({
  ...
});

// function should be like this to match encrypted password 
UserSchema.methods.authenticate = function(password) {      
  return this.password === this.hashPassword(password);
}
                ## OR ##

// if you are not using any encryption in password code then function should be like this 
UserSchema.methods.authenticate = function(password) {      
  return this.password === password;
}


mongoose.model('User', UserSchema);
module.exports = mongoose.model('User');

Upvotes: 0

Moksedul Hoque Uzzol
Moksedul Hoque Uzzol

Reputation: 51

I got the same error. In my case I forgot to add userSchema.plugin(passportLocalMongoose); and don't forget to npm install passport-local-mongoosepackage. I hope this will help you. thanks

Upvotes: 3

hya
hya

Reputation: 1738

Your Mongoose model does not have authenticate method, but you can add it to your schema.

Example code:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;


var UserSchema = new Schema ({
  ...
});


UserSchema.methods.authenticate = function(password) {
  //implementation code goes here
}

mongoose.model('User', UserSchema);
module.exports = mongoose.model('User');

Or you could use Mongoose passport plugin

Upvotes: 3

Related Questions