Created
January 3, 2018 14:06
-
-
Save maticzav/3858c422b89864cc95273d53f7b823b3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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