The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/app/grant-github/server/index.js

47 lines
1.2 KiB

import { HTTP } from 'meteor/http';
import { Providers, GrantError } from '../../grant';
const userAgent = 'Meteor';
function getIdentity(accessToken) {
try {
return HTTP.get(
'https://api.github.com/user', {
headers: { 'User-Agent': userAgent }, // http://developer.github.com/v3/#user-agent-required
params: { access_token: accessToken },
}).data;
} catch (err) {
throw new GrantError(`Failed to fetch identity from Github. ${ err.message }`);
}
}
function getEmails(accessToken) {
try {
return HTTP.get(
'https://api.github.com/user/emails', {
headers: { 'User-Agent': userAgent }, // http://developer.github.com/v3/#user-agent-required
params: { access_token: accessToken },
}).data;
} catch (err) {
return [];
}
}
export function getUser(accessToken) {
const identity = getIdentity(accessToken);
const emails = getEmails(accessToken);
const primaryEmail = (emails || []).find((email) => email.primary === true);
return {
id: identity.id,
email: identity.email || (primaryEmail && primaryEmail.email) || '',
username: identity.login,
emails,
name: identity.name,
avatar: identity.avatar_url,
};
}
// Register GitHub OAuth
Providers.register('github', { scope: ['user', 'user:email'] }, getUser);