forked from TanStack/tanstack.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
58 lines (48 loc) · 1.36 KB
/
github.ts
File metadata and controls
58 lines (48 loc) · 1.36 KB
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
50
51
52
53
54
55
56
57
58
import axios from 'axios'
import { Octokit } from '@octokit/rest'
import { graphql } from '@octokit/graphql'
export const GITHUB_ORG = 'TanStack'
export const octokit = new Octokit({
auth: process.env.GITHUB_AUTH_TOKEN,
userAgent: 'TanStack.com',
})
export const graphqlWithAuth = graphql.defaults({
headers: {
authorization: `token ${process.env.GITHUB_AUTH_TOKEN}`,
},
})
const githubClientID = 'Iv1.3aa8d13a4a3fde91'
const githubClientSecret = 'e2340f390f956b6fbfb9c6f85100d6cfe07f29a8'
export async function exchangeGithubCodeForToken({ code, state, redirectUrl }) {
try {
const { data } = await axios.post(
'https://github.com/login/oauth/access_token',
{
client_id: githubClientID,
client_secret: githubClientSecret,
code,
redirect_uri: redirectUrl,
state,
},
{
headers: {
Accept: 'application/json',
},
}
)
return data.access_token
} catch (err) {
console.error(err)
throw new Error('Unable to authenticate with GitHub. Please log in again.')
}
}
export async function getTeamBySlug(slug) {
const teams = await octokit.teams.list({
org: GITHUB_ORG,
})
const sponsorsTeam = teams.data.find((x) => x.slug === slug)
if (!sponsorsTeam) {
throw new Error(`Cannot find team "${slug}" in the organization`)
}
return sponsorsTeam
}