Skip to content

Instantly share code, notes, and snippets.

@maticzav
Created January 3, 2018 14:06
Show Gist options
  • Save maticzav/3858c422b89864cc95273d53f7b823b3 to your computer and use it in GitHub Desktop.
Save maticzav/3858c422b89864cc95273d53f7b823b3 to your computer and use it in GitHub Desktop.
// src/github.ts
import * as fetch from 'isomorphic-fetch'
export async function getGithubToken(githubCode: string): Promise<string> {
const endpoint = 'https://github.com/login/oauth/access_token'
const data = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
client_id: process.env.GITHUB_CLIENT_ID,
client_secret: process.env.GITHUB_CLIENT_SECRET,
code: githubCode,
})
})
.then(response => response.json())
if (data.error) {
throw new Error(JSON.stringify(data.error))
}
return data.access_token
}
export async function getGithubUser(githubToken: string): Promise<GithubUser> {
const endpoint = `https://api.github.com/user?access_token=${githubToken}`
const data = await fetch(endpoint)
.then(response => response.json())
if (data.error) {
throw new Error(JSON.stringify(data.error))
}
return data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment