From d609d73a41e1f62ab1c4f788229d029d1d8c6d7b Mon Sep 17 00:00:00 2001 From: Warren Parad Date: Fri, 27 Sep 2024 12:17:36 +0200 Subject: [PATCH] Use timeout === 0 to immediately fetch token. --- src/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/index.js b/src/index.js index 0336967..8241cc9 100644 --- a/src/index.js +++ b/src/index.js @@ -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();