简体   繁体   中英

passport-facebook returns 'undefined' profile object

I am implementing passport-facebook authentication in my application. I have implemented the following code.

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);
                });
            }
        });
    });
}));

login button

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

In the callback function of facebook strategy, I am always getting the profile as undefined and so the error is thrown at run time. Where am i going wrong?

Following is the log for reference.

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

I found the mistake I was making. It was with the app registration on Facebook developers website. Facebook expects to know the domain and Site URL while registration itself. I had not filled that information before. Once I filled that, issue got resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM