@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/vue/index.js CHANGED
@@ -149,6 +149,20 @@ var StandaloneOAuth2Auth = class {
149
149
  }
150
150
  };
151
151
 
152
+ // src/core/jwt.ts
153
+ function parseJwtExp(accessToken) {
154
+ const parts = accessToken?.split(".");
155
+ if (!parts || parts.length < 2) return null;
156
+ try {
157
+ const payload = JSON.parse(
158
+ atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
159
+ );
160
+ return typeof payload["exp"] === "number" ? payload["exp"] : null;
161
+ } catch {
162
+ return null;
163
+ }
164
+ }
165
+
152
166
  // src/core/iframe-auth.ts
153
167
  function postToHost(data) {
154
168
  const rnwv = window.ReactNativeWebView;
@@ -381,10 +395,11 @@ var IframeAuth = class {
381
395
  }
382
396
  handleSignin(signinData) {
383
397
  if (!signinData.accessToken) return;
398
+ const exp = parseJwtExp(signinData.accessToken);
384
399
  const tokens = {
385
400
  accessToken: signinData.accessToken,
386
401
  refreshToken: signinData.refreshToken,
387
- expiresAt: Math.floor(Date.now() / 1e3) + 3600,
402
+ expiresAt: exp ?? Math.floor(Date.now() / 1e3) + 3600,
388
403
  scope: ""
389
404
  };
390
405
  this.onTokensReceived?.(tokens);
@@ -833,6 +848,8 @@ var AuthManager = class {
833
848
  guidyBridge = null;
834
849
  reactNativeAuth = null;
835
850
  tokenRefreshTimer = null;
851
+ /** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
852
+ refreshInFlight = null;
836
853
  listeners = /* @__PURE__ */ new Set();
837
854
  navigationHandlers = /* @__PURE__ */ new Set();
838
855
  notificationHandlers = /* @__PURE__ */ new Set();
@@ -1233,26 +1250,41 @@ var AuthManager = class {
1233
1250
  */
1234
1251
  async getValidToken() {
1235
1252
  if (!this.tokens) return null;
1236
- const expiresAt = this.resolveExpiry(this.tokens);
1237
- if (this.autoRefreshEnabled && expiresAt) {
1253
+ if (this.autoRefreshEnabled) {
1254
+ const expiresAt = this.resolveExpiry(this.tokens);
1238
1255
  const nowSec = Math.floor(Date.now() / 1e3);
1239
- if (nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS) {
1256
+ const needsRefresh = expiresAt === null || nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS;
1257
+ if (needsRefresh) {
1240
1258
  try {
1241
- const freshToken = await this.handleTokenRefresh();
1242
- return freshToken;
1259
+ return await this.handleTokenRefresh();
1243
1260
  } catch {
1244
- return null;
1261
+ return expiresAt === null ? this.tokens.accessToken : null;
1245
1262
  }
1246
1263
  }
1247
1264
  }
1248
1265
  return this.tokens.accessToken;
1249
1266
  }
1250
1267
  /**
1251
- * Handle token refresh depending on mode.
1268
+ * Handle token refresh depending on mode, sharing a single in-flight
1269
+ * request across concurrent callers. Multiple requests crossing the expiry
1270
+ * buffer at once (e.g. a dashboard firing several queries) would otherwise
1271
+ * each trigger their own refresh; with refresh-token rotation only the first
1272
+ * succeeds and the rest can leave us holding an expired token — exactly the
1273
+ * backend "JWTExpired" the proactive path is meant to prevent.
1274
+ */
1275
+ handleTokenRefresh() {
1276
+ if (this.refreshInFlight) return this.refreshInFlight;
1277
+ this.refreshInFlight = this.performTokenRefresh().finally(() => {
1278
+ this.refreshInFlight = null;
1279
+ });
1280
+ return this.refreshInFlight;
1281
+ }
1282
+ /**
1283
+ * Refresh depending on mode.
1252
1284
  * Standalone: use OAuth2Client.getValidAccessToken() which auto-refreshes.
1253
1285
  * Iframe: send postMessage to host requesting new tokens.
1254
1286
  */
1255
- async handleTokenRefresh() {
1287
+ async performTokenRefresh() {
1256
1288
  if (this.mode === "standalone" && this.standaloneAuth) {
1257
1289
  const newTokens = await this.standaloneAuth.refreshToken();
1258
1290
  this.tokens = newTokens;
@@ -1313,17 +1345,7 @@ var AuthManager = class {
1313
1345
  */
1314
1346
  resolveExpiry(tokens) {
1315
1347
  if (tokens.expiresAt) return tokens.expiresAt;
1316
- const parts = tokens.accessToken?.split(".");
1317
- if (parts?.length >= 2) {
1318
- try {
1319
- const payload = JSON.parse(
1320
- atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
1321
- );
1322
- if (typeof payload["exp"] === "number") return payload["exp"];
1323
- } catch {
1324
- }
1325
- }
1326
- return null;
1348
+ return parseJwtExp(tokens.accessToken);
1327
1349
  }
1328
1350
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1329
1351
  destroy() {