passport-facebook返回'undefined'个人资料对象

时间:2016-08-14 07:31:51

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

我在我的应用程序中实现了passport-facebook身份验证。我已经实现了以下代码。

routes.js

// route for facebook authentication and login
router.get('/facebook', passport.authenticate('facebook', { scope : ['email'] }));

// handle the callback after facebook has authenticated the user
router.get('/facebook/callback', passport.authenticate('facebook', { successRedirect : '/dashboard', failureRedirect : '/'}));

passport.js

passport.use(new FacebookStrategy({
    clientID        : config.facebookAuth.clientID,
    clientSecret    : config.facebookAuth.clientSecret,
    callbackURL     : config.facebookAuth.callbackURL,
    passReqToCallback : true,
    profileFields: ['id', 'emails', 'name']
},
function(token, refreshToken, profile, done){
    process.nextTick(function(){
        console.log(profile);
        User.findOne({ 'username' : profile.name.givenName }, function(err, user){
            if (err)
                return done(err); // user not found
            if (user) {
                return done(null, user); // user found, return that user
            }else{
                var newUser = new User();
                newUser.username = profile.name.givenName;
                newUser.email = profile.emails[0].value;
                newUser.token = token;
                newUser.password = "";

                // save user to the database
                newUser.save(function(err) {
                    if (err)
                        throw err;
                    // if successful, return the new user
                    return done(null, newUser);
                });
            }
        });
    });
}));

登录按钮

                <a name="facebookLogin" class="btn btn-block btn-lg" href="https://vote-center.herokuapp.com/users/facebook" role="button"><span><img src="images/facebook.png"></span></a>

在facebook策略的回调函数中,我总是将配置文件视为未定义,因此在运行时抛出错误。我哪里错了?

以下是供参考的日志。

2016-08-14T07:03:27.995224+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/users/facebook/callback?code=AQBxQ6EGmLjBMNayuBNuj_-sfI69qpPwAnF-i9BGQntcLzH0pxXnbbyi1RHSjDfvzmCLphZORiL-M-HajfDWJX65QFODlCXY5sHh5BLEw1fUj_PWsWZNZmQkUK-RkIGU2Ip9XgOyuG9oD3BQkNZRYe0UtvUzRtQuORO4R29OQ37aJV6RFqnYl_SWREZLvGUi5_QN_InZ7OkHxFTn8rIft2t8qtyJCP4h8UH2-2kXjJqahD_E48DHKaUnWvH9UTG-FaT8n2Wa9sRC8JVcGV6t12mH8ajB64b4Wk_96fVG8y11_7dw-7u9BLm4eUzyFr1ecN4" host=vote-center.herokuapp.com request_id=f688bbd9-d77f-446d-833a-9919b3629598 fwd="45.114.61.58" dyno=web.1 connect=0ms service=278ms status=503 bytes=0
2016-08-14T07:03:27.985465+00:00 app[web.1]: undefined
2016-08-14T07:03:27.987413+00:00 app[web.1]: /app/configuration/passport.js:25
2016-08-14T07:03:27.987429+00:00 app[web.1]:             User.findOne({ 'username' : profile.name.givenName }, function(err, user){
2016-08-14T07:03:27.987441+00:00 app[web.1]:                                                ^
2016-08-14T07:03:27.987453+00:00 app[web.1]: 
2016-08-14T07:03:27.987469+00:00 app[web.1]:     at _combinedTickCallback (internal/process/next_tick.js:67:7)
2016-08-14T07:03:27.987482+00:00 app[web.1]:     at process._tickCallback (internal/process/next_tick.js:98:9)
2016-08-14T07:03:28.008758+00:00 app[web.1]: npm ERR! Linux 3.13.0-91-generic
2016-08-14T07:03:28.009344+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2016-08-14T07:03:28.011084+00:00 app[web.1]: npm ERR! Failed at the MEN@0.0.0 start script 'node ./bin/www'.
2016-08-14T07:03:28.012358+00:00 app[web.1]: npm ERR!     npm bugs MEN
2016-08-14T07:03:28.012908+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2016-08-14T07:03:28.017783+00:00 app[web.1]: npm ERR!     /app/npm-debug.log
2016-08-14T07:03:28.151473+00:00 heroku[web.1]: Process exited with status 1
2016-08-14T07:03:28.157074+00:00 heroku[web.1]: State changed from up to crashed

1 个答案:

答案 0 :(得分:0)

我发现了我犯的错误。这是在Facebook开发者网站上的应用程序注册。 Facebook希望在注册时知道域名和站点URL。我之前没有填写过这些信息。一旦我填写了,问题就解决了。