Configure Passport to accept request without body?

徘徊边缘 提交于 2019-12-08 04:46:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!