Last active
December 29, 2016 14:39
-
-
Save mastilver/6b9762aff79f1d99d6bc128c73c95402 to your computer and use it in GitHub Desktop.
auth0-github-recipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(accessToken, ctx, cb) { | |
request.get('https://api.github.com/user', { | |
headers: { | |
'User-Agent': 'Auth0-App', | |
'Authorization': 'Bearer ' + accessToken | |
} | |
}, function(e, r, body) { | |
if (e) return cb(e); | |
if (r.statusCode !== 200) return cb(new Error('StatusCode: ' + r.statusCode)); | |
const user = JSON.parse(body); | |
request.get('https://api.github.com/user/emails', { | |
headers: { | |
'User-Agent': 'Auth0-App', | |
'Authorization': 'Bearer ' + accessToken | |
} | |
}, function(e, r, body) { | |
if (e) return cb(e); | |
if (r.statusCode !== 200) return cb(new Error('StatusCode: ' + r.statusCode)); | |
const emails = JSON.parse(body); | |
const mainEmail = emails.find(x => x.email === user.email); | |
const email_verified = mainEmail ? mainEmail.verified : false; | |
cb(null, { | |
user_id: user.id, | |
email: user.email, | |
name: user.name, | |
picture: user.avatar_url, | |
nickname: user.login, | |
gravatar_id: user.gravatar_id, | |
url: user.url, | |
html_url: user.html_url, | |
followers_url: user.followers_url, | |
following_url: user.following_url, | |
gists_url: user.gists_url, | |
starred_url: user.starred_url, | |
subscriptions_url: user.subscriptions_url, | |
organizations_url: user.organizations_url, | |
repos_url: user.repos_url, | |
events_url: user.events_url, | |
received_events_url: user.received_events_url, | |
type: user.type, | |
site_admin: user.site_admin, | |
location: user.location, | |
public_repos: user.public_repos, | |
public_gists: user.public_gists, | |
followers: user.followers, | |
following: user.following, | |
emails: emails, | |
email_verified: email_verified, | |
token: accessToken, | |
scopes: ctx.scope.split(',') | |
}); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment