POST localhost/api/login
给出以下内容:
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指南,它已经基本了,但为什么会发生这种情况?
答案 0 :(得分:4)
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
});