gpt4 book ai didi

node.js - 错误: Unknown authentication strategy "[object Object]"

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:20 27 4
gpt4 key购买 nike

POST localhost/api/login 使用(正确的)数据 username=test password=test 给出以下内容:

Error: Unknown authentication strategy "[object Object]"
at attempt (/var/www/node_modules/passport/lib/middleware/authenticate.js:166:37)
at authenticate (/var/www/node_modules/passport/lib/middleware/authenticate.js:342:7)
at Layer.handle [as handle_request] (/var/www/node_modules/express/lib/router/layer.js:82:5)
at next (/var/www/node_modules/express/lib/router/route.js:110:13)
at Route.dispatch (/var/www/node_modules/express/lib/router/route.js:91:3)
at Layer.handle [as handle_request] (/var/www/node_modules/express/lib/router/layer.js:82:5)
at /var/www/node_modules/express/lib/router/index.js:267:22
at Function.proto.process_params (/var/www/node_modules/express/lib/router/index.js:321:12)
at next (/var/www/node_modules/express/lib/router/index.js:261:10)
at SessionStrategy.strategy.pass (/var/www/node_modules/passport/lib/middleware/authenticate.js:318:9)

路线:

app.post('/api/login', passport.authenticate(new LocalStrategy(function(username, password, done) {
db.User.findOne({username: username}, function(err, user) {
if (err) return done(err);
if (!user) return done(null, false, { message: 'Username or password incorrect' });
bcrypt.compare(password, user.passHash, function(err, res) {
if (err) return done(err);
if (!res) return done(null, false, { message: 'Username or password incorrect' });
return done(null, user);
})
})
})))

我正在运行 bodyParser,并且我知道它正在从我的注册路线运行。这里发生了什么?我一直在使用http://passportjs.org/guide指南并且已经掌握了基础知识,但是为什么会发生这种情况?

最佳答案

authenticate() 不接受对象,它接受一个包含您之前配置的策略名称的字符串。您的 new LocalStrategy() 被传递给 passport.use()

因此,您可以在中间件定义中执行此操作:

passport.use(new LocalStrategy(function(username, password, done) {
db.User.findOne({username: username}, function(err, user) {
if (err)
return done(err);
if (!user)
return done(null, false, { message: 'Username or password incorrect' });
bcrypt.compare(password, user.passHash, function(err, res) {
if (err)
return done(err);
if (!res)
return done(null, false, { message: 'Username or password incorrect' });
return done(null, user);
})
})
}));

然后你为你的路线做这样的事情:

app.post('/api/login',
passport.authenticate('local'),
function(req, res) {
// authentication successful
});

关于node.js - 错误: Unknown authentication strategy "[object Object]",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28311590/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com