-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrefresh-token.ts
More file actions
51 lines (44 loc) · 1.38 KB
/
refresh-token.ts
File metadata and controls
51 lines (44 loc) · 1.38 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
import fetch from 'node-fetch';
import { Handler, HandlerEvent } from '@netlify/functions';
const handler: Handler = async (event: HandlerEvent) => {
const client_id = process.env.VITE_GITHUB_CLIENT_ID!;
const client_secret = process.env.GITHUB_CLIENT_SECRET!;
const refresh_token = event.body ? JSON.parse(event.body).refreshToken : null;
if (!refresh_token) {
return {
statusCode: 400,
body: JSON.stringify({ error: 'refresh_token is required' })
};
}
try {
const response = await fetch('https://github.com/login/oauth/access_token', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
client_id,
client_secret,
refresh_token
})
});
const data: any = await response.json();
if (data.error) {
return {
statusCode: 400,
body: JSON.stringify({ error: data.error })
};
}
return {
statusCode: 200,
body: JSON.stringify(data)
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: 'Internal Server Error' })
};
}
};
export { handler };