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...
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