【问题标题】:Configure Passport to accept request without body?配置 Passport 以接受没有正文的请求?
【发布时间】:2014-12-13 22:02:24
【问题描述】:

我正在尝试设置一个 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');

护照版本:0.2.1

护照本地版本:1.0.0

【问题讨论】:

    标签: node.js passport.js


    【解决方案1】:

    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 ^
    }
    

    【讨论】:

    • 好主意!我看到 Passport 中间件处理了 400 错误,我建议中间件创建者在本地策略中添加一个新选项,以自己处理没有字段的请求:github.com/jaredhanson/passport-local/pull/91/files
    • 啊我对这个答案有点困惑,它如何改变任何东西,它如何防止护照在没有字段时抛出失败?
    • 我提了一个问题stackoverflow.com/questions/29146947/…如果你有空可以在这里给我回答一下或者澄清一下吗?
    • 问题是即使没有提供密码和用户名,我也想在护照策略中完成一个新用户
    猜你喜欢
    • 2018-06-28
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多