the accounts-github package is causing my meteor user to have a null email

久未见 提交于 2019-12-07 20:58:51

问题


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:


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.



来源:https://stackoverflow.com/questions/24689889/the-accounts-github-package-is-causing-my-meteor-user-to-have-a-null-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!