2017-09-01 88 views
0

我使用的護照Facebook的插件和我的測試腳本定義的策略如下:護照Facebook的策略在NextJS應用程序返回undefined值

// Configure Passport 
// --------------------------------------------------------------- 
passport.use(new LocalStrategy(User.authenticate())); 
passport.use(new FacebookStrategy({ 
    clientID: process.env.FACEBOOK_APP_ID, 
    clientSecret: process.env.FACEBOOK_APP_SECRET, 
    callbackURL: process.env.FACEBOOK_APP_CALLBACK, 
}, 
    function (accessToken, refreshToken, profile, done) { 
    console.log(profile); 
    } 
)); 
passport.serializeUser(User.serializeUser()); 
passport.deserializeUser(User.deserializeUser()); 
// --------------------------------------------------------------- 

我的認證途徑是:

// Facebook 
router.get('/auth/facebook', passport.authenticate('facebook')); 
router.get('/facebook/callback', 
    passport.authenticate('facebook', { 
    successRedirect: '/success', 
    failureRedirect: '/failure', 
    }), 
); 

我現在所要做的就是顯示Facebook成功驗證後返回的所有值。認證工作正常但Facebook似乎在profile對象被返回undefined大多數鍵:

{ id: '1234567812345677', 
    username: undefined, 
    displayName: 'John Doe', 
    name: 
    { familyName: undefined, 
    givenName: undefined, 
    middleName: undefined }, 
    gender: undefined, 
    profileUrl: undefined, 
    provider: 'facebook', 
    _raw: '{"name":"John Doe","id":"1234567812345677"}', 
    _json: { name: 'John Doe', id: '1234567812345677' } } 

我得到的是displayNameid;其他一切都是undefined。出了什麼問題?之前在這裏提出了一個非常類似的問題,但沒有一個答案(沒有被接受的答案)提供瞭解決問題的方法。

+0

完成guess-檢查您的應用範圍。您可能沒有完整的個人資料信息的權限。 –

+0

我沒有爲我的認證調用指定任何明確的'範圍'。不應該像* username *那樣可以作爲默認範圍的一部分?我在網上遇到的所有教程(包括PassportJS文檔)都會指示您指定一個範圍,以便您需要任何其他訪問權限,例如時間線等。 – TheLearner

+0

您是否找到了解決方案?我得到了同樣的問題:/ –

回答

0

我沒有這方面的經驗,但從看facebook-passport的readme,聽起來好像你需要在你的FacebookStrategy中使用profileFields。 :

The Facebook profile contains a lot of information about a user. By default, not all the fields in a profile are returned. The fields needed by an application can be indicated by setting the profileFields option.

new FacebookStrategy({ 
    clientID: FACEBOOK_APP_ID, 
    clientSecret: FACEBOOK_APP_SECRET, 
    callbackURL: "http://localhost:3000/auth/facebook/callback", 
    profileFields: ['id', 'displayName', 'photos', 'email'] 
}), ...) 

Refer to the User section of the Graph API Reference for the complete set of available fields.

+1

不幸的是,這是行不通的。拋出錯誤: 錯誤 at /home/ubuntu/nano/node_modules/passport-facebook/lib/strategy.js:165:21 at passBackControl(/ home/ubuntu/nano/node_modules/oauth/lib/oauth2。 js:132:9) IncomingMessage。 (/home/ubuntu/nano/node_modules/oauth/lib/oauth2.js:157:7) at emitNone(events.js:110:20) at IncomingMessage.emit(events.js:207:7) process.tickCallback(internal/process/next_tick.js:161:9)在endReadableNT(_stream_readable.js:1047:12) at _combinedTickCallback(internal/process/next_tick.js:102:11) – TheLearner