@docyrus/signin 0.14.0 → 0.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -151,6 +151,20 @@ var StandaloneOAuth2Auth = class {
151
151
  }
152
152
  };
153
153
 
154
+ // src/core/jwt.ts
155
+ function parseJwtExp(accessToken) {
156
+ const parts = accessToken?.split(".");
157
+ if (!parts || parts.length < 2) return null;
158
+ try {
159
+ const payload = JSON.parse(
160
+ atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
161
+ );
162
+ return typeof payload["exp"] === "number" ? payload["exp"] : null;
163
+ } catch {
164
+ return null;
165
+ }
166
+ }
167
+
154
168
  // src/core/iframe-auth.ts
155
169
  function postToHost(data) {
156
170
  const rnwv = window.ReactNativeWebView;
@@ -383,10 +397,11 @@ var IframeAuth = class {
383
397
  }
384
398
  handleSignin(signinData) {
385
399
  if (!signinData.accessToken) return;
400
+ const exp = parseJwtExp(signinData.accessToken);
386
401
  const tokens = {
387
402
  accessToken: signinData.accessToken,
388
403
  refreshToken: signinData.refreshToken,
389
- expiresAt: Math.floor(Date.now() / 1e3) + 3600,
404
+ expiresAt: exp ?? Math.floor(Date.now() / 1e3) + 3600,
390
405
  scope: ""
391
406
  };
392
407
  this.onTokensReceived?.(tokens);
@@ -841,6 +856,8 @@ var AuthManager = class {
841
856
  guidyBridge = null;
842
857
  reactNativeAuth = null;
843
858
  tokenRefreshTimer = null;
859
+ /** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
860
+ refreshInFlight = null;
844
861
  listeners = /* @__PURE__ */ new Set();
845
862
  navigationHandlers = /* @__PURE__ */ new Set();
846
863
  notificationHandlers = /* @__PURE__ */ new Set();
@@ -1241,26 +1258,41 @@ var AuthManager = class {
1241
1258
  */
1242
1259
  async getValidToken() {
1243
1260
  if (!this.tokens) return null;
1244
- const expiresAt = this.resolveExpiry(this.tokens);
1245
- if (this.autoRefreshEnabled && expiresAt) {
1261
+ if (this.autoRefreshEnabled) {
1262
+ const expiresAt = this.resolveExpiry(this.tokens);
1246
1263
  const nowSec = Math.floor(Date.now() / 1e3);
1247
- if (nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS) {
1264
+ const needsRefresh = expiresAt === null || nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS;
1265
+ if (needsRefresh) {
1248
1266
  try {
1249
- const freshToken = await this.handleTokenRefresh();
1250
- return freshToken;
1267
+ return await this.handleTokenRefresh();
1251
1268
  } catch {
1252
- return null;
1269
+ return expiresAt === null ? this.tokens.accessToken : null;
1253
1270
  }
1254
1271
  }
1255
1272
  }
1256
1273
  return this.tokens.accessToken;
1257
1274
  }
1258
1275
  /**
1259
- * Handle token refresh depending on mode.
1276
+ * Handle token refresh depending on mode, sharing a single in-flight
1277
+ * request across concurrent callers. Multiple requests crossing the expiry
1278
+ * buffer at once (e.g. a dashboard firing several queries) would otherwise
1279
+ * each trigger their own refresh; with refresh-token rotation only the first
1280
+ * succeeds and the rest can leave us holding an expired token — exactly the
1281
+ * backend "JWTExpired" the proactive path is meant to prevent.
1282
+ */
1283
+ handleTokenRefresh() {
1284
+ if (this.refreshInFlight) return this.refreshInFlight;
1285
+ this.refreshInFlight = this.performTokenRefresh().finally(() => {
1286
+ this.refreshInFlight = null;
1287
+ });
1288
+ return this.refreshInFlight;
1289
+ }
1290
+ /**
1291
+ * Refresh depending on mode.
1260
1292
  * Standalone: use OAuth2Client.getValidAccessToken() which auto-refreshes.
1261
1293
  * Iframe: send postMessage to host requesting new tokens.
1262
1294
  */
1263
- async handleTokenRefresh() {
1295
+ async performTokenRefresh() {
1264
1296
  if (this.mode === "standalone" && this.standaloneAuth) {
1265
1297
  const newTokens = await this.standaloneAuth.refreshToken();
1266
1298
  this.tokens = newTokens;
@@ -1321,17 +1353,7 @@ var AuthManager = class {
1321
1353
  */
1322
1354
  resolveExpiry(tokens) {
1323
1355
  if (tokens.expiresAt) return tokens.expiresAt;
1324
- const parts = tokens.accessToken?.split(".");
1325
- if (parts?.length >= 2) {
1326
- try {
1327
- const payload = JSON.parse(
1328
- atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
1329
- );
1330
- if (typeof payload["exp"] === "number") return payload["exp"];
1331
- } catch {
1332
- }
1333
- }
1334
- return null;
1356
+ return parseJwtExp(tokens.accessToken);
1335
1357
  }
1336
1358
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1337
1359
  destroy() {