javascriptnode.jsmongodbmongoosemongoose-plugins

passport mongoose Object #<Schema> has no method 'authenticate' yet I can see the method


I am running into an issue that doesn't make sense and can't figure out hoping someone can assist.

I am using a few mongoose plugins and now trying to integrate passport as well. I have added the passport-local-mongoose plugin as shown below, but I am getting errors that the method does not exist. When logging the schema out to the console, I can see the method listed, so I am not sure why it doesn't exist.

Code sample:

var mongoose = require('mongoose');
var timestamps = require('mongoose-timestamp');
var autoIncrement = require('mongoose-auto-increment');
var passport = require('passport');
var passportLocalMongoose = require('passport-local-mongoose');
var BasicStrategy = require('passport-http').BasicStrategy;

var usersSchema = new mongoose.Schema({
  firstName: String,
  lastName: String,
  email: String,
  organizationId: Number,
  description: String
});

module.exports = function(app,db){
  //mongoose setup options
  usersSchema.plugin(timestamps);  
  usersSchema.plugin(autoIncrement.plugin, { model: 'Users', field: 'id' });

  //passport authentication setup
  usersSchema.plugin(passportLocalMongoose);
  var Users = db.model('Users', usersSchema);
  console.log(usersSchema);

  passport.use(new BasicStrategy(usersSchema.authenticate()));

Console log excerpt as follows:

methods:
 { nextCount: [Function],
   resetCount: [Function],
   setPassword: [Function],
   authenticate: [Function] },
statics:
 { nextCount: [Function],
   resetCount: [Function],
   authenticate: [Function],
   serializeUser: [Function],
   deserializeUser: [Function],
   register: [Function],
   findByUsername: [Function],
   createStrategy: [Function] },

But I am getting the following error:

passport.use(new BasicStrategy(usersSchema.authenticate()));
                                             ^
TypeError: Object #<Schema> has no method 'authenticate'

I can see the method is available, so why am I not able to access it. I apologize if I am missing something basic, I am still learning some of these things. Thanks for any assistance.


Solution

  • The authenticate method is defined on the model not on the schema -

    passport.user(new  BasicStrategy(Users.authenticate());