Meteor.loginWithFacebook不存储电子邮件地址 [英] Meteor.loginWithFacebook not storing email address

查看:326
本文介绍了Meteor.loginWithFacebook不存储电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

  • accounts-password
  • accounts-facebook
  • service-configuration

在服务器上:

ServiceConfiguration.configurations.remove({
    service: 'facebook'
});

ServiceConfiguration.configurations.upsert(
    { service: 'facebook' },
    { $set: {
        appId: 'xxxxxxxxxxxxxxxx',
        secret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
      }
    }
);

在客户端上:

Meteor.loginWithFacebook({requestPermissions: ['email']}, function(error){
  if (error) {
    throwError('Could not log in');
  } else {
    // success
  }
});

此配置提示用户进行Facebook验证并可以访问电子邮件,并且不返回任何错误.使用正确的名称和ID存储新用户.但是此电子邮件未存储在用户对象中.

This configuration prompts the user for Facebook verification with access to email and returns no errors. A new user is stores with the correct name and ID. But this e-mail is not stored in the user object.

这是我从外壳中获取用户时得到的.

This is what I get when i fetch a user from the shell.

{ _id: 'xxxxxxxxxxxxxxxxx',
  createdAt: Mon Jul 13 2015 13:36:21 GMT+0200 (CEST),
  services: 
   { facebook: 
      { accessToken: 'xxxxxxxxxxxxxxxxxxxxx...',
        expiresAt: 1441971380621,
        id: 'xxxxxxxxxxxxxxxxx',
        name: 'xxxx xxxxxx' },
     resume: { loginTokens: [Object] } },
  profile: { name: 'xxxx xxxxxx' } }

为什么不存储来自Facebook的电子邮件地址?

Why is the email address from Facebook not being stored?

推荐答案

虽然我已将问题报告给流星我暂时找到了一种快速修复方法.

While I have reported the issue to Meteor I've found a quick fix for the time being.

在服务器上运行以下命令:

On the server run this:

Accounts.onCreateUser(function(options, user) {
  if (user.hasOwnProperty('services') && user.services.hasOwnProperty('facebook')  ) {
    var fb = user.services.facebook;
    var result = Meteor.http.get('https://graph.facebook.com/v2.4/' + fb.id + '?access_token=' + fb.accessToken + '&fields=name,email');

    if (!result.error) {
      _.extend(user, {
        "emails": [{"address": result.data.email, "verified": false}],
        "profile": {"name": result.data.name}
      });
    }
  }

  return user;
});

先前的代码可以工作,但是由于它会导致其他登录方法出现问题,因此我采用了另一种方法:

The previous code works, but since it causes problems with other login methods I went with another approach:

在客户端中,当用户通过Facebook进行身份验证时,我在服务器上调用了一个函数:

In the client I call a function on the server when the user authenticates with Facebook:

Meteor.loginWithFacebook({requestPermissions: ['email']}, function(error){
  if (error) {
    //error
  } else {
    Meteor.call('fbAddEmail');
  }
});

然后在服务器上:

Meteor.startup(function () {
  Meteor.methods({
    fbAddEmail: function() {
      var user = Meteor.user();
      if (user.hasOwnProperty('services') && user.services.hasOwnProperty('facebook')  ) {
        var fb = user.services.facebook;
        var result = Meteor.http.get('https://graph.facebook.com/v2.4/' + fb.id + '?access_token=' + fb.accessToken + '&fields=name,email');

        if (!result.error) {
          Meteor.users.update({_id: user._id}, {
            $addToSet: { "emails": {
              'address': result.data.email,
              'verified': false
            }}
          });
        }
      }
    }
  });
});

这篇关于Meteor.loginWithFacebook不存储电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆