最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - LocalStrategy is not a constructor - Stack Overflow

programmeradmin6浏览0评论

I'm using passport.js for authentication, and trying to setup a local strategy. However, whenever I run my code I get an error saying localStrategy is not a constructor.

Code:

// config/passport.js

// load all the things we need
var localStrategy   = require('passport-local');

// load up the user model
var User            = require('../app/models/user');

// expose this function to our app using module.exports
module.exports = function(passport) {

// =========================================================================
// passport session setup ==================================================
// =========================================================================
// required for persistent login sessions
// passport needs ability to serialize and unserialize users out of session

// used to serialize the user for the session
passport.serializeUser(function(user, done) {
    done(null, user.id);
});

// used to deserialize the user
passport.deserializeUser(function(id, done) {
    User.findById(id, function(err, user) {
        done(err, user);
    });
});

// =========================================================================
// LOCAL SIGNUP ============================================================
// =========================================================================
// we are using named strategies since we have one for login and one for signup
// by default, if there was no name, it would just be called 'local'

 var authenticate = User.authenticate();
 console.log(authenticate);
 passport.use('local-signup', new localStrategy(authenticate));


};

When I run the app, I get:

TypeError: localStrategy is not a constructor
    at module.exports (/home/ubuntu/workspace/config/passport.js:38:35)
    at Object.<anonymous> (/home/ubuntu/workspace/server.js:19:29)
    at Module._pile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3

If you look here They do use new LocalStrategy. And I have another app that uses this too, and it works =S

Help?

I'm using passport.js for authentication, and trying to setup a local strategy. However, whenever I run my code I get an error saying localStrategy is not a constructor.

Code:

// config/passport.js

// load all the things we need
var localStrategy   = require('passport-local');

// load up the user model
var User            = require('../app/models/user');

// expose this function to our app using module.exports
module.exports = function(passport) {

// =========================================================================
// passport session setup ==================================================
// =========================================================================
// required for persistent login sessions
// passport needs ability to serialize and unserialize users out of session

// used to serialize the user for the session
passport.serializeUser(function(user, done) {
    done(null, user.id);
});

// used to deserialize the user
passport.deserializeUser(function(id, done) {
    User.findById(id, function(err, user) {
        done(err, user);
    });
});

// =========================================================================
// LOCAL SIGNUP ============================================================
// =========================================================================
// we are using named strategies since we have one for login and one for signup
// by default, if there was no name, it would just be called 'local'

 var authenticate = User.authenticate();
 console.log(authenticate);
 passport.use('local-signup', new localStrategy(authenticate));


};

When I run the app, I get:

TypeError: localStrategy is not a constructor
    at module.exports (/home/ubuntu/workspace/config/passport.js:38:35)
    at Object.<anonymous> (/home/ubuntu/workspace/server.js:19:29)
    at Module._pile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3

If you look here https://www.npmjs./package/passport-local They do use new LocalStrategy. And I have another app that uses this too, and it works =S

Help?

Share Improve this question edited Oct 28, 2017 at 20:49 Zac 1,3033 gold badges17 silver badges28 bronze badges asked Oct 28, 2017 at 19:17 AsoolAsool 14.2k8 gold badges37 silver badges53 bronze badges 2
  • I copied the code here too scotch.io/tutorials/… ... same error – Asool Commented Oct 28, 2017 at 19:21
  • 1 Javascript is case sensitive. Try new LocalStrategy(...) (note the capital L) – Dirk Commented Oct 28, 2017 at 20:35
Add a ment  | 

1 Answer 1

Reset to default 17

You just missed including Strategy in your require

Change

var localStrategy = require('passport-local');

to

var localStrategy = require('passport-local').Strategy;

发布评论

评论列表(0)

  1. 暂无评论