Facebook通过护照认证。无法读取未定义错误的属性“0”

时间:2017-02-11 01:17:53

标签: node.js mongodb express passport.js passport-facebook

我正在学习将Facebook社交认证添加到应用程序。一切正常,直到我真的尝试使用Facebook登录我的测试应用程序。我没有路由到个人资料页面,而是收到此错误:

Cannot read property '0' of undefined

TypeError: Cannot read property '0' of undefined
    at Strategy.generateOrFindUser [as _verify] (C:\projects\Side            Projects\passyapp\app.js:16:19)
    at C:\projects\Side Projects\passyapp\node_modules\passport-  oauth2\lib\strategy.js:193:24
    at C:\projects\Side Projects\passyapp\node_modules\passport-  github\lib\strategy.js:174:7
    at passBackControl (C:\projects\Side Projects\passyapp\node_modules\oauth\lib\oauth2.js:134:9)
    at IncomingMessage.<anonymous> (C:\projects\Side Projects\passyapp\node_modules\oauth\lib\oauth2.js:157:7)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

以下是我的APP.JS文件中的一个小册子:

function generateOrFindUser(accessToken, refreshToken, profile, done){
  if(profile.emails[0]) {
    User.findOneAndUpdate(
      { email: profile.emails[0] },
      {
        name: profile.displayName || profile.username,
        email: profile.emails[0].value,
        photo: profile.photos[0].value
      },
      {
        upsert: true
      },
    done
  );
  } else {
    var noEmailError = new Error("Your email privacy settings prevent you from signing in.");
    done(noEmailError, null);
  }
}

passport.use(new FacebookStrategy({
  clientID: process.env.FACEBOOK_APP_ID,
  clientSecret: process.env.FACEBOOK_APP_SECRET,
  callbackURL: "http://localhost:3000/auth/facebook/return",
  profileFields: ['email', 'id', 'displayName', 'photos']
},
  generateOrFindUser)
);


//in order for passport to handle sessions you need to implement the two methods
//directly below
passport.serializeUser(function(user, done) {
  done(null, user._id);
});

passport.deserializeUser(function(userId, done) {
  User.findById(userId, function(err, done) {
    //done(err, user);
  });
});

这是我的用户架构

var UserSchema = new mongoose.Schema({
  email: {
    type: String,
    required: true,
    trim: true,
    unique: true,
  },
  name: {
    type: String,
    required: true,
    trim: true,
  },
  favoriteBook: {
    type: String,
    required: false,
    trim: true
  },
  photo: {
    type: String,
    required: true,
    trim: true
  }
});

我希望这不是浪费你忙碌的编码时间。 非常感谢任何帮助尝试。

2 个答案:

答案 0 :(得分:1)

编辑:更好的答案

我猜它是profile.emails对象(根据行号无法分辨,因为您的代码是一个片段)。在该函数之前,您应该拥有看起来像这样的代码。

您需要确保两件事:

<强> 1 当您致电passport.authenticate时,请务必请求电子邮件范围,如下所示: app.route('/auth/facebook').get(passport.authenticate('facebook', { scope: ['email']}));

<强> 2

passport.use(new FacebookStrategy({
  clientID: facebookConfig.clientID,
  clientSecret: facebookConfig.clientSecret,
  callbackURL: facebookConfig.callbackURL,
  profileFields   : ['id', 'name', 'email'],
  },
  function generateOrFindUser...

您的profileFields数组包含电子邮件

非常重要

答案 1 :(得分:0)

  1. 首先,设置您的profileFields部分。

    'facebookAuth' : {
       'clientID'      : 'your App ID',
       'clientSecret'  : 'your App Secret',
       'callbackURL'   : 'your Callback URL',
       'profileFields'   : ['emails']
    }
    
  2. 现在,无论我们在配置护照代码时,都需要提取这些详细信息。

    passport.use(new FacebookStrategy({
        // pull in our app id and secret from our auth.js file
        clientID        : configAuth.facebookAuth.clientID,
        clientSecret    : configAuth.facebookAuth.clientSecret,
        callbackURL     : configAuth.facebookAuth.callbackURL,
        profileFields   : configAuth.facebookAuth.profileFields
    }