Skip to content

Commit

Permalink
Use timeout === 0 to immediately fetch token.
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Sep 27, 2024
1 parent 2cd5ecc commit d609d73
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,20 @@ class LoginClient {
* @throws {TokenTimeout} After the timeout if no session was found. By default waits for 5000 for another thread to continue the session, after which if still no token exists, will throw
*/
async ensureToken(options) {
// When the time is set to zero, don't race the promises, instead just directly check if the token likely exists and return it. Otherwise throw.
// * We do this to avoid a scenario where the Promise.race(setTimeout(0)) immediately returns first even if the session is available.
if (options?.timeoutInMillis === 0) {
const userIdentity = this.getUserIdentity();
const cookies = cookieManager.parse(document.cookie);
if (userIdentity) {
return cookies.authorization !== 'undefined' && cookies.authorization;
}

const error = Error('No token retrieved after timeout');
error.code = 'TokenTimeout';
throw error;
}

// Using this function blocks all ensureToken calls on a single session continuation, this is required.
await this.userSessionExists();

Expand Down

0 comments on commit d609d73

Please sign in to comment.