-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauthHelper.ts
More file actions
47 lines (42 loc) · 1.37 KB
/
authHelper.ts
File metadata and controls
47 lines (42 loc) · 1.37 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
interface TokenResponse {
access_token: string;
refresh_token: string;
expires_in: number;
refresh_token_expires_in: number;
}
export function storeToken({
access_token,
refresh_token,
expires_in,
refresh_token_expires_in
}: TokenResponse) {
localStorage.setItem('github_access_token', access_token);
localStorage.setItem('github_refresh_token', refresh_token);
localStorage.setItem('github_token_expiry', (Date.now() + expires_in * 1000).toString());
localStorage.setItem('github_refresh_token_expiry', (Date.now() + refresh_token_expires_in * 1000).toString());
}
async function refreshAccessToken() {
const refreshToken = localStorage.getItem('github_refresh_token');
if (!refreshToken) {
throw new Error('No refresh token found');
}
fetch("/.netlify/functions/refresh-token", {
method: "POST",
body: JSON.stringify({ refreshToken}),
})
.then((response) => response.json())
.then((data) => {
storeToken(data)
})
}
export async function getAccessToken() {
const token = localStorage.getItem('github_access_token');
const expiry = localStorage.getItem('github_token_expiry');
if (!token || !expiry) {
throw new Error('No token found');
}
if (parseInt(expiry) < Date.now()) {
await refreshAccessToken();
}
return token ?? '';
}