Files
meteor/packages/accounts-github/github_server.js
David Glasser 7b758a0c9b Rename and refactor Accounts.updateOrCreateUser.
This is an internal function only used by OAuth implementations and the
equivalent, so rename to a more specific name:
Accounts.updateOrCreateUserFromExternalService.

Change the signature to directly take serviceName and serviceData instead of a
nested data structure with a very specific structure. Similarly, change
Accounts.oauth.registerService's handleOauthRequest callback to un-nest the
service data.

Throw errors on misuse (if you try to use it with the "password" or
soon-to-be-introduced "resume" services, or if you don't provide an id).

Avoid doing no-op user updates if there is nothing new in "extra".
2012-10-08 09:18:55 -07:00

46 lines
1.3 KiB
JavaScript

(function () {
Accounts.oauth.registerService('github', 2, function(query) {
var accessToken = getAccessToken(query);
var identity = getIdentity(accessToken);
return {
serviceData: {
id: identity.id,
accessToken: accessToken,
email: identity.email,
username: identity.login
},
extra: {profile: {name: identity.name}}
};
});
var getAccessToken = function (query) {
var config = Accounts.configuration.findOne({service: 'github'});
if (!config)
throw new Accounts.ConfigError("Service not configured");
var result = Meteor.http.post(
"https://github.com/login/oauth/access_token", {headers: {Accept: 'application/json'}, params: {
code: query.code,
client_id: config.clientId,
client_secret: config.secret,
redirect_uri: Meteor.absoluteUrl("_oauth/github?close"),
state: query.state
}});
if (result.error) // if the http response was an error
throw result.error;
if (result.data.error) // if the http response was a json object with an error attribute
throw result.data;
return result.data.access_token;
};
var getIdentity = function (accessToken) {
var result = Meteor.http.get(
"https://api.github.com/user",
{params: {access_token: accessToken}});
if (result.error)
throw result.error;
return result.data;
};
}) ();