2016-07-11 104 views
1

我正在嘗試使用來自node.js的護照設置Facebook身份驗證。當我運行應用程序並轉到/ auth/facebook時,我會轉到Facebook頁面以授權Facebook返回我的電子郵件。但是,當我點擊確定後,我得到一個頁面(回調),說:「沒有電子郵件與此帳戶關聯!」根據代碼,這意味着profile.emails爲空。我錯過了什麼?Node.js Passport Facebook身份驗證沒有電子郵件返回

這些是當我啓動應用程序時顯示的警告。我不知道他們是否是問題。

express-session deprecated undefined resave option; provide resave option auth.js:44:37 
express-session deprecated undefined saveUninitialized option; provide saveUninitialized option auth.js:44:37 

以下是授權碼。我正在閱讀edx的介紹MEAN堆棧課程M101X。

function setupAuth(User, app) { 
    var passport = require('passport'); 
    var FacebookStrategy = require('passport-facebook').Strategy; 

    // High level serialize/de-serialize configuration for passport 
    passport.serializeUser(function(user, done) { 
    done(null, user._id); 
    }); 

    passport.deserializeUser(function(id, done) { 
    User. 
     findOne({ _id : id }). 
     exec(done); 
    }); 

    // Facebook-specific 
    passport.use(new FacebookStrategy(
    { 
     clientID: process.env.FACEBOOK_CLIENT_ID, 
     clientSecret: process.env.FACEBOOK_CLIENT_SECRET, 
     callbackURL: 'http://localhost:3000/auth/facebook/callback' 
    }, 
    function(accessToken, refreshToken, profile, done) { 
     if (!profile.emails || !profile.emails.length) { 
     return done('No emails associated with this account!'); 
     } 

     User.findOneAndUpdate(
     { 'data.oauth': profile.id }, 
     { 
      $set: { 
      'profile.username': profile.emails[0].value, 
      'profile.picture': 'http://graph.facebook.com/' + 
       profile.id.toString() + '/picture?type=large' 
      } 
     }, 
     { 'new': true, upsert: true, runValidators: true }, 
     function(error, user) { 
      done(error, user); 
     }); 
    })); 

    // Express middlewares 
    app.use(require('express-session')({ 
    secret: 'this is a secret' 
    })); 
    app.use(passport.initialize()); 
    app.use(passport.session()); 

    // Express routes for auth 
    app.get('/auth/facebook', 
    passport.authenticate('facebook', { scope: ['email'] })); 

    app.get('/auth/facebook/callback', 
    passport.authenticate('facebook', { failureRedirect: '/fail' }), 
    function(req, res) { 
     res.send('Welcome, ' + req.user.profile.username); 
    }); 
} 

module.exports = setupAuth; 

回答

1

你嘗試加入profileFields的passport.use塊內像建議在課程的討論選項卡,在「Facebook的身份驗證問題」?

相關問題