-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth0.js
37 lines (31 loc) · 965 Bytes
/
auth0.js
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
let auth0AccessToken;
let auth0ExpiryTime;
const getAuth0AccessToken = async () => {
if (auth0AccessToken && auth0ExpiryTime) {
const currentTime = Math.floor(Date.now() / 1000);
if (currentTime < auth0ExpiryTime) {
return auth0AccessToken;
}
}
const responseData = await fetch(
`https://${process.env.AUTH0_DOMAIN}/oauth/token`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
audience: process.env.AUTH0_AUDIENCE,
grant_type: "client_credentials",
client_id: process.env.AUTH0_CLIENT_ID,
client_secret: process.env.AUTH0_CLIENT_SECRET,
}),
}
);
const response = await responseData.json();
auth0AccessToken = response.access_token;
// Set expiry time, 5 minutes before the actual expiry time
auth0ExpiryTime = response.expires_in - 300;
return auth0AccessToken;
};
export { getAuth0AccessToken };