Node.js Passport Facebook身份验证没有回复电子邮件

时间:2016-07-11 00:34:21

标签: node.js mongodb passport.js mean-stack passport-facebook

我正在尝试使用来自node.js的护照设置facebook身份验证。当我运行应用程序并转到/ auth / facebook时,我会转到Facebook页面,允许facebook返回我的电子邮件。但在我点击确定后,我得到一个页面(回调),上面写着“没有与此帐户关联的电子邮件!”根据代码,这意味着profile.emails为null。我错过了什么?

这些是我启动应用时出现的警告。我不知道他们是不是问题。

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 个答案:

答案 0 :(得分:1)

您是否尝试在passport.use块中添加profileFields,就像该课程的讨论标签中的建议一样," Facebook身份验证问题"?