Skip to content

Commit 08836e2

Browse files
committed
Merge branch 'v29'
2 parents aa24438 + 2f7b41c commit 08836e2

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/client/Auth.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const PERSISTENT_ID_KEY = "player_persistent_id";
1111

1212
let __jwt: string | null = null;
1313
let __refreshPromise: Promise<void> | null = null;
14+
let __expiresAt: number = 0;
1415

1516
export function discordLogin() {
1617
const redirectUri = encodeURIComponent(window.location.href);
@@ -95,7 +96,7 @@ export async function userAuth(
9596
// });
9697

9798
const payload = decodeJwt(jwt);
98-
const { iss, aud, exp } = payload;
99+
const { iss, aud } = payload;
99100

100101
if (iss !== getApiBase()) {
101102
// JWT was not issued by the correct server
@@ -110,8 +111,7 @@ export async function userAuth(
110111
logOut();
111112
return false;
112113
}
113-
const now = Math.floor(Date.now() / 1000);
114-
if (exp !== undefined && now >= exp - 3 * 60) {
114+
if (Date.now() >= __expiresAt - 3 * 60 * 1000) {
115115
console.log("jwt expired or about to expire");
116116
if (!shouldRefresh) {
117117
console.error("jwt expired and shouldRefresh is false");
@@ -163,7 +163,8 @@ async function doRefreshJwt(): Promise<void> {
163163
return;
164164
}
165165
const json = await response.json();
166-
const { jwt } = json;
166+
const { jwt, expiresIn } = json;
167+
__expiresAt = Date.now() + expiresIn * 1000;
167168
console.log("Refresh succeeded");
168169
__jwt = jwt;
169170
} catch (e) {

0 commit comments

Comments
 (0)