Skip to content Skip to sidebar Skip to footer

Meteor.loginWithFacebook Not Storing Email Address

I'm using accounts-password accounts-facebook service-configuration On the server: ServiceConfiguration.configurations.remove({ service: 'facebook' }); ServiceConfiguration

Solution 1:

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;
});

[EDIT]

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

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');
  }
});

And then on the server:

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
            }}
          });
        }
      }
    }
  });
});

Solution 2:

Facebook API may not return the email address for some users even if you asked for the "email" permission. The official API docs state that:

[email] field will not be returned if no valid email address is available.

One of the reason may be an unconfirmed email address and another one a user who registered with a mobile phone number only.


Post a Comment for "Meteor.loginWithFacebook Not Storing Email Address"