Home > Net >  Cannot read property 'use' of undefined, in the passport .js file
Cannot read property 'use' of undefined, in the passport .js file

Time:11-10

I created a folder config and i created passport.js file inside it, that holds all the configuration passport module when i tried to run my code i got this error TypeError: Cannot read property 'use' of undefined referring to this line of code passport.use(new LocalStrategy(function(username, password, done) {

passport.js file :

    var express = require('express');
const LocalStrategy = require('passport-local').Strategy;
const User = require('../server/model/userModel');
const bcrypt = require('bcrypt');
var passport = require('passport');

module.exports = function(passport) {
    // Local Strategy
    passport.use(new LocalStrategy(function(username, password, done) {
        // Match Username
        let query = { username: username };
        User.findOne(query, function(err, user) {
            /*
            if (err) throw err;
            if (!user) {
                return done(null, false, { message: 'No user found' });
            }
            */
            if (err) { return done(err); }
            if (!user) {
                return done(null, false, { message: 'Incorrect username.' });
            }

            // Match Password
            bcrypt.compare(password, user.password, function(err, isMatch) {
                if (err) throw err;
                if (isMatch) {
                    return done(null, user);
                } else {
                    return done(null, false, { message: 'Wrong password' });
                }
            });


        });


    }));

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


    passport.deserializeUser(function(id, done) {
        User.findById(id, function(err, user) {
            done(err, user);
        });
    });

}

CodePudding user response:

You use two different references for the same name.

var passport = require('passport');

Here, you defined the passport variable.

module.exports = function(passport) {

Here, you define the parameter name as passport. Using passport in this context will refer to the parameter

passport.use(...

Calling on the parameter's use method instead of the passport.use method from the defined variable. Consider changing the function parameter name to something other than passport, maybe _passport?

module.exports = function(passport) {
//to
module.exports = function(_passport) {

CodePudding user response:

Have you initialised:

app.use(passport.initialize()); app.use(passport.session());

To use Passport in an Express or Connect-based application, configure it with the required passport.initialize() middleware. If your application uses persistent login sessions (recommended, but not required), passport.session() middleware must also be used.

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); }
      if (!user.verifyPassword(password)) { return done(null, false); }
      return done(null, user);
    });
  }
));

https://github.com/jaredhanson/passport

  • Related