gpt4 book ai didi

javascript - LocalStrategy 不是构造函数

转载 作者:搜寻专家 更新时间:2023-10-31 23:31:15 24 4
gpt4 key购买 nike

我正在使用 passport.js 进行身份验证,并尝试设置本地策略。但是,每当我运行我的代码时,我都会收到一条错误消息,提示 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

帮忙吗?

最佳答案

你只是错过了在你的需求中包含策略

改变

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

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

关于javascript - LocalStrategy 不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46993529/

24 4 0
文章推荐: node.js - 我可以在 1 个终端中运行两个正在进行的 npm 命令吗
文章推荐: php - 使用 .htaccess 处理 404 错误
文章推荐: php - Google map 地理编码问题 : Certain Encoded URLs Failing
文章推荐: node.js - 如何修复 Webpack 相关错误 : Cannot assign to read only property 'exports' of object '#' ?