@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.
@@ -2,7 +2,7 @@ import { OAuth2Tokens } from '@docyrus/api-client';
2
2
  import { D as DocyrusAuthConfig } from '../types-D4ZAVj-B.js';
3
3
  export { A as AclOperation, a as AuthMode, b as AuthSessionResult, c as AuthStatus, d as DocyrusAclRule, e as DocyrusAuthContextValue, f as DocyrusRole, g as DocyrusUser, O as OpenAuthSessionFn, P as PermissionConfig } from '../types-D4ZAVj-B.js';
4
4
  export { D as DocyrusAuthContext, a as DocyrusAuthProvider, b as DocyrusAuthProviderProps, u as useDocyrusAuth, c as useDocyrusClient } from '../use-docyrus-client-o9nPwayd.js';
5
- export { A as AuthManager, D as DEFAULT_API_URL, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, h as hasPermission, b as hasRole } from '../constants-DtyJKJgJ.js';
5
+ export { A as AuthManager, D as DEFAULT_API_URL, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, h as hasPermission, b as hasRole } from '../constants-DDOgvbL5.js';
6
6
  import 'react';
7
7
 
8
8
  /**
@@ -228,6 +228,20 @@ var StandaloneOAuth2Auth = class {
228
228
  }
229
229
  };
230
230
 
231
+ // src/core/jwt.ts
232
+ function parseJwtExp(accessToken) {
233
+ const parts = accessToken?.split(".");
234
+ if (!parts || parts.length < 2) return null;
235
+ try {
236
+ const payload = JSON.parse(
237
+ atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
238
+ );
239
+ return typeof payload["exp"] === "number" ? payload["exp"] : null;
240
+ } catch {
241
+ return null;
242
+ }
243
+ }
244
+
231
245
  // src/core/iframe-auth.ts
232
246
  function postToHost(data) {
233
247
  const rnwv = window.ReactNativeWebView;
@@ -460,10 +474,11 @@ var IframeAuth = class {
460
474
  }
461
475
  handleSignin(signinData) {
462
476
  if (!signinData.accessToken) return;
477
+ const exp = parseJwtExp(signinData.accessToken);
463
478
  const tokens = {
464
479
  accessToken: signinData.accessToken,
465
480
  refreshToken: signinData.refreshToken,
466
- expiresAt: Math.floor(Date.now() / 1e3) + 3600,
481
+ expiresAt: exp ?? Math.floor(Date.now() / 1e3) + 3600,
467
482
  scope: ""
468
483
  };
469
484
  this.onTokensReceived?.(tokens);
@@ -836,6 +851,8 @@ var AuthManager = class {
836
851
  guidyBridge = null;
837
852
  reactNativeAuth = null;
838
853
  tokenRefreshTimer = null;
854
+ /** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
855
+ refreshInFlight = null;
839
856
  listeners = /* @__PURE__ */ new Set();
840
857
  navigationHandlers = /* @__PURE__ */ new Set();
841
858
  notificationHandlers = /* @__PURE__ */ new Set();
@@ -1236,26 +1253,41 @@ var AuthManager = class {
1236
1253
  */
1237
1254
  async getValidToken() {
1238
1255
  if (!this.tokens) return null;
1239
- const expiresAt = this.resolveExpiry(this.tokens);
1240
- if (this.autoRefreshEnabled && expiresAt) {
1256
+ if (this.autoRefreshEnabled) {
1257
+ const expiresAt = this.resolveExpiry(this.tokens);
1241
1258
  const nowSec = Math.floor(Date.now() / 1e3);
1242
- if (nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS) {
1259
+ const needsRefresh = expiresAt === null || nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS;
1260
+ if (needsRefresh) {
1243
1261
  try {
1244
- const freshToken = await this.handleTokenRefresh();
1245
- return freshToken;
1262
+ return await this.handleTokenRefresh();
1246
1263
  } catch {
1247
- return null;
1264
+ return expiresAt === null ? this.tokens.accessToken : null;
1248
1265
  }
1249
1266
  }
1250
1267
  }
1251
1268
  return this.tokens.accessToken;
1252
1269
  }
1253
1270
  /**
1254
- * Handle token refresh depending on mode.
1271
+ * Handle token refresh depending on mode, sharing a single in-flight
1272
+ * request across concurrent callers. Multiple requests crossing the expiry
1273
+ * buffer at once (e.g. a dashboard firing several queries) would otherwise
1274
+ * each trigger their own refresh; with refresh-token rotation only the first
1275
+ * succeeds and the rest can leave us holding an expired token — exactly the
1276
+ * backend "JWTExpired" the proactive path is meant to prevent.
1277
+ */
1278
+ handleTokenRefresh() {
1279
+ if (this.refreshInFlight) return this.refreshInFlight;
1280
+ this.refreshInFlight = this.performTokenRefresh().finally(() => {
1281
+ this.refreshInFlight = null;
1282
+ });
1283
+ return this.refreshInFlight;
1284
+ }
1285
+ /**
1286
+ * Refresh depending on mode.
1255
1287
  * Standalone: use OAuth2Client.getValidAccessToken() which auto-refreshes.
1256
1288
  * Iframe: send postMessage to host requesting new tokens.
1257
1289
  */
1258
- async handleTokenRefresh() {
1290
+ async performTokenRefresh() {
1259
1291
  if (this.mode === "standalone" && this.standaloneAuth) {
1260
1292
  const newTokens = await this.standaloneAuth.refreshToken();
1261
1293
  this.tokens = newTokens;
@@ -1316,17 +1348,7 @@ var AuthManager = class {
1316
1348
  */
1317
1349
  resolveExpiry(tokens) {
1318
1350
  if (tokens.expiresAt) return tokens.expiresAt;
1319
- const parts = tokens.accessToken?.split(".");
1320
- if (parts?.length >= 2) {
1321
- try {
1322
- const payload = JSON.parse(
1323
- atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
1324
- );
1325
- if (typeof payload["exp"] === "number") return payload["exp"];
1326
- } catch {
1327
- }
1328
- }
1329
- return null;
1351
+ return parseJwtExp(tokens.accessToken);
1330
1352
  }
1331
1353
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1332
1354
  destroy() {