我正在尝试设置一个 LocalStrategy,可以在没有请求正文中给出的凭据的情况下登录
app.post('/signin', passport.authenticate('local-signin', {failureFlash : true}), function (req, res) {
res.send(req.user);
});
策略如下:
passport
.use('local-signin', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function (req, email, password, done) {
if (email.length > 0 && password.legnth > 0) {
/** Do some stuff with credentials **/
}
else {
console.log('method2');
/** Other method to log in with request headers...**/
}
});
不幸的是,当我向 /signin
发布没有正文的请求时,出现状态 400 错误,并且未加载策略(在示例中我没有 method2
在控制台中编写)。
有没有办法将 Passport 配置为接受没有正文的请求?
作为信息,我使用 AngularJS 前端,通过 $http.post('/signin'); 发出请求
Passport 版本:0.2.1
Passport 本地版本:1.0.0
最佳答案
将 passport.authenticate
中间件包装在另一个类似的东西中
app.post('/signin', myPass, function(req,res){res.send(req.user)});
function myPass(req, res, next){
// do your thing
passport.authenticate('local-signin', {failureFlash : true})(req, res, next);
// ^ this returns a middleware that you gotta call here now ^
}
关于node.js - 配置 Passport 接受没有正文的请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27464091/