问题
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);
});
Here's the strategy :
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...**/
}
});
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).
Is there a way to configure Passport to accept request without body?
For information, I use AngularJS front-end, requests made with $http.post('/signin');
passport version : 0.2.1
passport-local version : 1.0.0
回答1:
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 ^
}
来源:https://stackoverflow.com/questions/27464091/configure-passport-to-accept-request-without-body