2016-08-14 22 views
0

我在我的應用程序中實施passport-facebook身份驗證。我已經實現了下面的代碼。護照-Facebook返回'undefined'個人資料對象

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="https://stackoverflow.com/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 [email protected] start script 'node ./bin/www'. 
2016-08-14T07:03:28.: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 

回答

0

我發現我犯的錯誤。這是在Facebook開發者網站上的應用程序註冊。 Facebook希望在註冊時瞭解域名和網站URL。我以前沒有填寫過這些信息。一旦我填補了這個問題,問題就解決了。

相關問題