-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathgithub-adapter.ts
More file actions
49 lines (39 loc) · 990 Bytes
/
github-adapter.ts
File metadata and controls
49 lines (39 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import axios from 'axios'
import {config} from './config'
interface GitHubUser {
id: number
name: string
}
interface AccessTokenResponse {
access_token: string
}
interface UserResponse {
id: number
name: string
}
const TOKEN_URL = 'https://github.com/login/oauth/access_token'
const USER_URL = 'https://api.github.com/user'
export async function getGitHubUser(code: string) {
const token = await getAccessToken(code)
return getUser(token)
}
async function getAccessToken(code: string) {
const response = await axios.post<AccessTokenResponse>(
TOKEN_URL,
{
client_id: config.gitHubClientId,
client_secret: config.gitHubClientSecret,
code,
},
{
headers: {Accept: 'application/json'},
}
)
return response.data.access_token
}
async function getUser(token: string) {
const response = await axios.get<UserResponse>(USER_URL, {
headers: {Authorization: `Bearer ${token}`},
})
return response.data as GitHubUser
}