2017-10-28 151 views
2

我正在使用passport.js對於身份驗證,並嘗試設置本地策略。然而,每當我運行我的代碼時,我得到一個錯誤,說localStrategy不是一個構造函數。LocalStrategy不是構造函數

代碼

// 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)); 


}; 

當我運行應用程序,我得到:

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._compile (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 

如果你看看這裏 https://www.npmjs.com/package/passport-local 他們使用新LocalStrategy。我還有另一個應用程序也使用它,它的工作原理= S

幫助?

+0

我在這裏複製了代碼https://scotch.io/tutorials/easy-node-authentication-setup-and-local#toc-handling-signupregistration ...同樣的錯誤 – Asool

+0

Javascript區分大小寫。嘗試'新的LocalStrategy(...)'(注意大寫'L') – Dirk

回答

3

你只是錯過了包括戰略的需要

變化

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

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