【问题标题】:Passport Facebook Authentication Returns Undefined?Passport Facebook 身份验证返回未定义?
【发布时间】:2018-10-15 06:45:47
【问题描述】:

我的facebook策略如下:

passport.use(new facebookStrategy({

    clientID: envVars.facebook.clientID,
    clientSecret: envVars.facebook.clientSecret,
    callbackURL: envVars.facebook.callbackURL,
    passReqToCallback : true,
    profileFields: ['id', 'emails', 'displayName']
},

function(token, refreshToken, profile, done) {
    console.log(profile); //prints out undefined
}
));

而我的路线是这样处理的:

router.get('/facebook', passport.authenticate('facebook'));

router.get('/auth/facebook/redirect', passport.authenticate('facebook'), 
function(req, res){
    res.redirect("/profile");
});

我的代码成功地做到了将用户引导到 Facebook,在那里他们被提示允许我的应用访问他们的数据。一旦接受,我的 console.log(profile) 会触发,但即使用户接受许可,它也会打印出 undefined ?我已经搜索了文档,但似乎无法弄清楚我哪里出错了。

【问题讨论】:

    标签: node.js oauth-2.0 passport.js passport-facebook


    【解决方案1】:

    似乎是 passReqToCallback 的问题。请参考这个 SO 问题:Using PassportJS, how does one pass additional form fields to the local authentication strategy?

    在您的情况下,删除 passReqToCallback:true 标志。

    passport.use(new facebookStrategy({
    
        clientID: envVars.facebook.clientID,
        clientSecret: envVars.facebook.clientSecret,
        callbackURL: envVars.facebook.callbackURL,
        profileFields: ['id', 'emails', 'displayName']
    },
    

    另一种选择是修改您的回调并使用第一个参数,即请求对象。

    function(req, email, password, done) {
        // now you can check req.body.foo
    }
    

    【讨论】:

    • 啊,谢谢!删除返回的数据似乎很奇怪。
    猜你喜欢
    • 2016-11-12
    • 1970-01-01
    • 2015-03-16
    • 2018-01-05
    • 1970-01-01
    • 2016-09-28
    • 2020-03-15
    • 2019-10-07
    • 2019-11-29
    相关资源
    最近更新 更多