Skip to content

Commit f2d6715

Browse files
committed
Add "isAuthSessionReady" method
1 parent dd6b51a commit f2d6715

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/private/model/AuthManagerInterface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,9 @@ export interface AuthManagerInterface {
112112
* Checks if an authenticated Bytescale API and Bytescale CDN session is active.
113113
*/
114114
isAuthSessionActive: () => boolean;
115+
116+
/**
117+
* Checks if an authenticated Bytescale API and Bytescale CDN session is active and ready to authenticate HTTP requests.
118+
*/
119+
isAuthSessionReady: () => boolean;
115120
}

src/public/browser/AuthManagerBrowser.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class AuthManagerImpl implements AuthManagerInterface {
2727
return AuthSessionState.getSession() !== undefined;
2828
}
2929

30+
isAuthSessionReady(): boolean {
31+
return AuthSessionState.getSession()?.accessToken !== undefined;
32+
}
33+
3034
async beginAuthSession(params: BeginAuthSessionParams): Promise<void> {
3135
const session = await this.authSessionMutex.safe(async () => {
3236
// We check both 'session' and 'sessionDisposing' here, as we don't want to call 'beginAuthSession' until the session is fully disposed.
@@ -120,6 +124,11 @@ class AuthManagerImpl implements AuthManagerInterface {
120124
session.authServiceWorker,
121125
this.serviceWorkerScriptFieldName
122126
);
127+
128+
// Allow time for the service worker to receive and process the message. Since this is asynchronous and not
129+
// synchronized, we need to wait for a sufficient amount of time to ensure the service worker is ready to
130+
// authenticate requests, so that after 'beginAuthSession' completes, users can start making requests.
131+
await new Promise(resolve => setTimeout(resolve, 100));
123132
}
124133

125134
const desiredTtl = setTokenResult.ttlSeconds - this.refreshBeforeExpirySeconds;
@@ -131,6 +140,7 @@ class AuthManagerImpl implements AuthManagerInterface {
131140
// There's no need to print a warning for this: it's OK to silently request the JWT before it expires. Also, this is 24 days in this case!
132141
timeout = Math.min(timeout, this.maxJwtTtlSeconds);
133142

143+
// Set this at the end, as it's also used to signal 'isAuthSessionReady', so must be set after configuring the Service Worker, etc.
134144
session.accessToken = setTokenResult.accessToken;
135145
} catch (e) {
136146
// Use 'warn' instead of 'error' since this happens frequently, i.e. user goes through a tunnel, and some customers report these errors to systems like Sentry, so we don't want to spam.

src/public/node/AuthManagerNode.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class AuthManagerImpl implements AuthManagerInterface {
1313
isAuthSessionActive(): boolean {
1414
return false;
1515
}
16+
17+
isAuthSessionReady(): boolean {
18+
return false;
19+
}
1620
}
1721

1822
/**

0 commit comments

Comments
 (0)