@docyrus/signin 0.13.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);
@@ -451,6 +466,7 @@ function isVisible(node) {
451
466
  }
452
467
  var GuidyBridge = class {
453
468
  send;
469
+ routes;
454
470
  started = false;
455
471
  observer = null;
456
472
  scanTimer = null;
@@ -458,8 +474,14 @@ var GuidyBridge = class {
458
474
  highlightTimer = null;
459
475
  clickTimer = null;
460
476
  annotationTimer = null;
461
- constructor(send) {
477
+ constructor(send, routes = []) {
462
478
  this.send = send;
479
+ this.routes = routes;
480
+ }
481
+ /** Replace the declared navigable routes and re-post them to the host. */
482
+ setRoutes(routes) {
483
+ this.routes = routes;
484
+ this.postRoutes();
463
485
  }
464
486
  /**
465
487
  * Begin reporting the element inventory and watching for changes.
@@ -470,6 +492,7 @@ var GuidyBridge = class {
470
492
  this.started = true;
471
493
  this.ensureStyle();
472
494
  this.postElements();
495
+ this.postRoutes();
473
496
  this.observer = new MutationObserver(() => this.scheduleScan());
474
497
  this.observer.observe(document.body, {
475
498
  childList: true,
@@ -509,6 +532,7 @@ var GuidyBridge = class {
509
532
  handleCommand(message) {
510
533
  if (message.type === "guidy:scan") {
511
534
  this.postElements();
535
+ this.postRoutes();
512
536
  return;
513
537
  }
514
538
  if (message.type === "guidy:point") {
@@ -545,6 +569,10 @@ var GuidyBridge = class {
545
569
  elements: this.collectElements()
546
570
  });
547
571
  }
572
+ postRoutes() {
573
+ if (!this.started) return;
574
+ this.send({ type: "guidy:routes", routes: this.routes });
575
+ }
548
576
  scheduleScan() {
549
577
  if (this.scanTimer) return;
550
578
  this.scanTimer = setTimeout(() => {
@@ -828,6 +856,8 @@ var AuthManager = class {
828
856
  guidyBridge = null;
829
857
  reactNativeAuth = null;
830
858
  tokenRefreshTimer = null;
859
+ /** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
860
+ refreshInFlight = null;
831
861
  listeners = /* @__PURE__ */ new Set();
832
862
  navigationHandlers = /* @__PURE__ */ new Set();
833
863
  notificationHandlers = /* @__PURE__ */ new Set();
@@ -1000,11 +1030,22 @@ var AuthManager = class {
1000
1030
  */
1001
1031
  startGuidyBridge() {
1002
1032
  if (!this.iframeAuth || this.guidyBridge) return;
1003
- const bridge = new GuidyBridge((message) => this.iframeAuth?.sendToHost(message));
1033
+ const bridge = new GuidyBridge(
1034
+ (message) => this.iframeAuth?.sendToHost(message),
1035
+ this.config.guidyRoutes ?? []
1036
+ );
1004
1037
  this.iframeAuth.onGuidyCommand((message) => bridge.handleCommand(message));
1005
1038
  bridge.start();
1006
1039
  this.guidyBridge = bridge;
1007
1040
  }
1041
+ /**
1042
+ * Iframe mode: replace the routes the Guidy bridge advertises to the host.
1043
+ * No-op unless the bridge is running (`enableGuidyBridge`).
1044
+ */
1045
+ setGuidyRoutes(routes) {
1046
+ this.config.guidyRoutes = routes;
1047
+ this.guidyBridge?.setRoutes(routes);
1048
+ }
1008
1049
  /**
1009
1050
  * Iframe/WebView mode: start auto-syncing the embedded app's route
1010
1051
  * to the host. Patches history methods and listens for popstate /
@@ -1217,26 +1258,41 @@ var AuthManager = class {
1217
1258
  */
1218
1259
  async getValidToken() {
1219
1260
  if (!this.tokens) return null;
1220
- const expiresAt = this.resolveExpiry(this.tokens);
1221
- if (this.autoRefreshEnabled && expiresAt) {
1261
+ if (this.autoRefreshEnabled) {
1262
+ const expiresAt = this.resolveExpiry(this.tokens);
1222
1263
  const nowSec = Math.floor(Date.now() / 1e3);
1223
- if (nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS) {
1264
+ const needsRefresh = expiresAt === null || nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS;
1265
+ if (needsRefresh) {
1224
1266
  try {
1225
- const freshToken = await this.handleTokenRefresh();
1226
- return freshToken;
1267
+ return await this.handleTokenRefresh();
1227
1268
  } catch {
1228
- return null;
1269
+ return expiresAt === null ? this.tokens.accessToken : null;
1229
1270
  }
1230
1271
  }
1231
1272
  }
1232
1273
  return this.tokens.accessToken;
1233
1274
  }
1234
1275
  /**
1235
- * 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.
1236
1292
  * Standalone: use OAuth2Client.getValidAccessToken() which auto-refreshes.
1237
1293
  * Iframe: send postMessage to host requesting new tokens.
1238
1294
  */
1239
- async handleTokenRefresh() {
1295
+ async performTokenRefresh() {
1240
1296
  if (this.mode === "standalone" && this.standaloneAuth) {
1241
1297
  const newTokens = await this.standaloneAuth.refreshToken();
1242
1298
  this.tokens = newTokens;
@@ -1297,17 +1353,7 @@ var AuthManager = class {
1297
1353
  */
1298
1354
  resolveExpiry(tokens) {
1299
1355
  if (tokens.expiresAt) return tokens.expiresAt;
1300
- const parts = tokens.accessToken?.split(".");
1301
- if (parts?.length >= 2) {
1302
- try {
1303
- const payload = JSON.parse(
1304
- atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
1305
- );
1306
- if (typeof payload["exp"] === "number") return payload["exp"];
1307
- } catch {
1308
- }
1309
- }
1310
- return null;
1356
+ return parseJwtExp(tokens.accessToken);
1311
1357
  }
1312
1358
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1313
1359
  destroy() {
@@ -1507,6 +1553,11 @@ function DocyrusAuthProvider({
1507
1553
  managerRef.current?.notifyHostRouteChange(payload);
1508
1554
  };
1509
1555
  }, []);
1556
+ const setGuidyRoutes = useMemo(() => {
1557
+ return (routes) => {
1558
+ managerRef.current?.setGuidyRoutes(routes);
1559
+ };
1560
+ }, []);
1510
1561
  const value = useMemo(() => ({
1511
1562
  status,
1512
1563
  mode,
@@ -1524,6 +1575,7 @@ function DocyrusAuthProvider({
1524
1575
  onHostNotification,
1525
1576
  enableHostRouteSync,
1526
1577
  notifyRouteChange,
1578
+ setGuidyRoutes,
1527
1579
  error
1528
1580
  }), [
1529
1581
  status,
@@ -1542,6 +1594,7 @@ function DocyrusAuthProvider({
1542
1594
  onHostNotification,
1543
1595
  enableHostRouteSync,
1544
1596
  notifyRouteChange,
1597
+ setGuidyRoutes,
1545
1598
  error
1546
1599
  ]);
1547
1600
  return /* @__PURE__ */ jsx(DocyrusAuthContext, { value, children });
@@ -1587,6 +1640,12 @@ function useDocyrusHostNotification(handler) {
1587
1640
  });
1588
1641
  }, [onHostNotification]);
1589
1642
  }
1643
+ function useDocyrusGuidyRoutes(routes) {
1644
+ const { setGuidyRoutes } = useDocyrusAuth();
1645
+ useEffect(() => {
1646
+ setGuidyRoutes(routes);
1647
+ }, [setGuidyRoutes, routes]);
1648
+ }
1590
1649
  var ICON_SIZE = 20;
1591
1650
  function DocyrusLogo({ size = ICON_SIZE }) {
1592
1651
  return /* @__PURE__ */ jsx(
@@ -1671,6 +1730,6 @@ function SignInButton({
1671
1730
  );
1672
1731
  }
1673
1732
 
1674
- export { AuthManager, DEFAULT_API_URL, DEFAULT_CALLBACK_PATH, DEFAULT_OAUTH_SCOPES, DEFAULT_SSR_COOKIE_KEY, DocyrusAuthContext, DocyrusAuthProvider, GuidyBridge, IframeAuth, SignInButton, StandaloneOAuth2Auth, clearTokenCookie, detectAuthMode, getAllRoles, getTokenFromCookie, hasPermission, hasRole, syncTokenToCookie, useDocyrusAuth, useDocyrusClient, useDocyrusHostNavigation, useDocyrusHostNotification, useDocyrusHostRouteSync };
1733
+ export { AuthManager, DEFAULT_API_URL, DEFAULT_CALLBACK_PATH, DEFAULT_OAUTH_SCOPES, DEFAULT_SSR_COOKIE_KEY, DocyrusAuthContext, DocyrusAuthProvider, GuidyBridge, IframeAuth, SignInButton, StandaloneOAuth2Auth, clearTokenCookie, detectAuthMode, getAllRoles, getTokenFromCookie, hasPermission, hasRole, syncTokenToCookie, useDocyrusAuth, useDocyrusClient, useDocyrusGuidyRoutes, useDocyrusHostNavigation, useDocyrusHostNotification, useDocyrusHostRouteSync };
1675
1734
  //# sourceMappingURL=index.js.map
1676
1735
  //# sourceMappingURL=index.js.map