Skip to content

Commit

Permalink
Add "isAuthSessionReady" method
Browse files Browse the repository at this point in the history
  • Loading branch information
ljwagerfield committed Jul 16, 2024
1 parent dd6b51a commit f2d6715
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/private/model/AuthManagerInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,9 @@ export interface AuthManagerInterface {
* Checks if an authenticated Bytescale API and Bytescale CDN session is active.
*/
isAuthSessionActive: () => boolean;

/**
* Checks if an authenticated Bytescale API and Bytescale CDN session is active and ready to authenticate HTTP requests.
*/
isAuthSessionReady: () => boolean;
}
10 changes: 10 additions & 0 deletions src/public/browser/AuthManagerBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class AuthManagerImpl implements AuthManagerInterface {
return AuthSessionState.getSession() !== undefined;
}

isAuthSessionReady(): boolean {
return AuthSessionState.getSession()?.accessToken !== undefined;
}

async beginAuthSession(params: BeginAuthSessionParams): Promise<void> {
const session = await this.authSessionMutex.safe(async () => {
// We check both 'session' and 'sessionDisposing' here, as we don't want to call 'beginAuthSession' until the session is fully disposed.
Expand Down Expand Up @@ -120,6 +124,11 @@ class AuthManagerImpl implements AuthManagerInterface {
session.authServiceWorker,
this.serviceWorkerScriptFieldName
);

// Allow time for the service worker to receive and process the message. Since this is asynchronous and not
// synchronized, we need to wait for a sufficient amount of time to ensure the service worker is ready to
// authenticate requests, so that after 'beginAuthSession' completes, users can start making requests.
await new Promise(resolve => setTimeout(resolve, 100));
}

const desiredTtl = setTokenResult.ttlSeconds - this.refreshBeforeExpirySeconds;
Expand All @@ -131,6 +140,7 @@ class AuthManagerImpl implements AuthManagerInterface {
// 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!
timeout = Math.min(timeout, this.maxJwtTtlSeconds);

// Set this at the end, as it's also used to signal 'isAuthSessionReady', so must be set after configuring the Service Worker, etc.
session.accessToken = setTokenResult.accessToken;
} catch (e) {
// 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.
Expand Down
4 changes: 4 additions & 0 deletions src/public/node/AuthManagerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class AuthManagerImpl implements AuthManagerInterface {
isAuthSessionActive(): boolean {
return false;
}

isAuthSessionReady(): boolean {
return false;
}
}

/**
Expand Down

0 comments on commit f2d6715

Please sign in to comment.