2

I've added accounts-github to my meteor app, but when I try to access Meteor.user.services.github.email all i get is null. Even though I know the email is set in my github account. What am I doing wrong? The field is there, seems like accounts-github should just fetch the email for me...

1 Answer 1

8
+100

From the github api docs:

Note: The returned email is the user’s publicly visible email address (or null if the user has not specified a public email address in their profile).

To get the private email address you need to add the user:email scope to your app.

If you're using accounts-ui its just

Client side

Accounts.ui.config({
    requestPermissions: {
        github: ['user:email']
    }
});

Update

I've tried the code above and It gives a couple of issues. It appears github no longer sends the email data along with the other OAuth data. Adding this as well as the above (for permissions) fixes it:

What it does is gets the email data separately in a request to github and adds it to your user when they log in.

Add the github api package

meteor add mrt:github-api

Server Side Code

Accounts.onLogin(function(info) {
    var user = info.user;
    if(user) {

    var github = new GitHub({
          version: "3.0.0", // required
          timeout: 5000     // optional
      });

      github.authenticate({
        type: "oauth",
        token: user.services.github.accessToken
      });

      try {
        var result = github.user.getEmails({user: user.services.github.username});

        var email = _(result).findWhere({primary: true});

        Meteor.users.update({
          _id: user._id
        },
        {
          $set: {
            'profile.email': email.email,
            'services.github.email': email.email
          }
        })
      }
      catch(e) {
        console.log(e.message);
      }
    }
  });

You can then access the email address as normal in {{currentUser.profile.email}} (html), Meteor.user().profile.email, as well as in the services.github object.

There is an advantage to doing this too, The email field will stay up to date if they change it on github and log in again.

7
  • Tried this and it's still giving me null for the email. I did meteor reset to start from scratch and still can't get it. I see that the github popup on login has the new permissions (I added a couple others to test it too).
    – Ben
    Commented Jul 12, 2014 at 6:10
  • 1
    @Ben after a bit of tinkering I got it working :) - Updated answer
    – Tarang
    Commented Jul 13, 2014 at 10:57
  • I'll try this in a bit, thanks for your help, but shouldn't it just do this out of the box? The accounts-google package does, for example...
    – Ben
    Commented Jul 14, 2014 at 21:48
  • confirmed that this works. still, wish I didn't have to do this. you should do a pull request on accounts-github that does this out of the box...
    – Ben
    Commented Jul 14, 2014 at 22:34
  • Yeah, it seems like this could be helped by an upstream fix in the accounts-github package. Lets open a support request on the Meteor issue tracker to see how this might be resolved. Commented Jun 5, 2015 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.