-
Notifications
You must be signed in to change notification settings - Fork 1
/
refresh.ts
54 lines (44 loc) · 1.17 KB
/
refresh.ts
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
import { setRefetchBeforeExp, projectkey } from "./config.ts";
const [tick, refreshToken] = Deno.args;
/**
* write.ts
*/
async function writeJson(
path: string,
data: Record<string, unknown>
): Promise<string> {
try {
await Deno.writeTextFile(path, JSON.stringify(data));
return path;
} catch (e) {
return e.message;
}
}
/*
* Refetch token using refresh token
*/
export const refetchToken = async (params?: { refreshToken?: string }) => {
const { refreshToken } = params ?? {};
const baseUrl = "securetoken.googleapis.com/v1/token";
const firebase = await fetch(`https://${baseUrl}?key=${projectkey}`, {
headers: {
contentType: "application/json",
},
method: "POST",
body: JSON.stringify({
refresh_token: refreshToken,
grant_type: "refresh_token",
}),
});
const json = await firebase.json();
json && (await writeJson("./firebase_auth_token.json", json));
await setRefetchBeforeExp({
expiresIn: json.expires_in,
refreshToken: json.refresh_token,
});
await Deno.kill(Deno.pid, Deno.Signal.SIGINT);
};
setTimeout(
async () => await refetchToken({ refreshToken }),
Math.max(Number(tick) - 1, 1)
);