我正在尝试将 OAuth2 登录添加到基于 Node/ExpressJS/MongoDB/PassportJS 构建的应用程序中。
我可以使用此方法成功登录,但我无法获取某些信息。我能够访问的唯一内容是email
,name
和fbId
字段 - 我目前无法从这里访问生日、喜欢、家乡和其他信息。
我的 userSchema 设置如下:
var userSchema = new mongoose.Schema({
fbId: String,
name: String,
email: { type:String, lowercase: true },
gender: String,
birthday: String,
hometown: Object,
location: Object,
likes: Array
});
这是在我的 Express 服务器上运行的代码:
passport.use(new FacebookStrategy({
clientID: config.development.fb.appId,
clientSecret: config.development.fb.appSecret,
callbackURL: config.development.fb.url + 'fbauthed'
},
function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
var query = User.findOne({ 'fbId': profile.id });
query.exec(function(err, oldUser) {
if (oldUser) {
console.log('Existing user: ' + oldUser.name + ' found and logged in!');
done(null, oldUser);
} else {
var newUser = new User();
newUser.fbId = profile.id;
newUser.name = profile.displayName;
newUser.email = profile.emails[0].value;
newUser.gender = profile.gender;
newUser.birthday = profile.birthday;
newUser.hometown = profile.hometown;
newUser.location = profile.location;
newUser.likes = profile.likes;
newUser.save(function(err) {
if (err) { return done(err); }
console.log('New user: ' + newUser.name + ' created and logged in!');
done(null, newUser);
});
}
});
});
}
));
app.get('/fbauth', passport.authenticate('facebook', { scope: 'email' }));
app.get('/fbauthed', passport.authenticate('facebook',{ failureRedirect: '/' }), function(req, res){}
当我尝试登录并尝试访问这些属性时,我得到:
fbId: String, // Defined
name: String, // Defined
email: { type:String, lowercase: true }, // Defined
gender: String, // Defined
birthday: String, // UNDEFINED
hometown: Object, // UNDEFINED
location: Object, // UNDEFINED
likes: Array // UNDEFINED
我是否试图错误地访问它们?我在这里做错了什么?