问题描述
我正在尝试设置一个 LocalStrategy,可以在没有请求正文中提供凭据的情况下登录
I'm trying to set a LocalStrategy with the possibility to login without credentials given in request body
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
写在控制台).
Unfortunately, when I post a request to /signin
without body, I have a status 400 error, and the strategy isn't loaded (in the example I don't have method2
written in console).
有没有办法将 Passport 配置为接受没有正文的请求?
Is there a way to configure Passport to accept request without body?
有关信息,我使用 AngularJS 前端,请求使用 $http.post('/signin');
For information, I use AngularJS front-end, requests made with $http.post('/signin');
护照版本:0.2.1
护照本地版本:1.0.0
推荐答案
将 passport.authenticate
中间件包装在另一个类似这样的东西中
Wrap the passport.authenticate
middleware inside another something like this
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 ^
}
这篇关于配置 Passport 以接受没有正文的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!