@docyrus/signin 0.14.0 → 0.15.0

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;
@@ -259,6 +273,19 @@ var IframeAuth = class {
259
273
  url: `${path}${search}${hash}`
260
274
  });
261
275
  }
276
+ /**
277
+ * Ask the host shell to navigate to a path/URL. The reverse of the
278
+ * host → app `navigation` message: posts a `navigation-request` over the
279
+ * same validated channel. The host decides how to honour it.
280
+ */
281
+ requestHostNavigation(url, options) {
282
+ if (this.stopped) return;
283
+ if (typeof url !== "string" || url.length === 0) return;
284
+ const message = { type: "navigation-request", url };
285
+ if (options?.replace) message.replace = true;
286
+ if (options?.newTab) message.newTab = true;
287
+ this.sendToHost(message);
288
+ }
262
289
  teardownRouteSync() {
263
290
  if (!this.routeSyncEnabled) return;
264
291
  if (typeof window !== "undefined") {
@@ -383,10 +410,11 @@ var IframeAuth = class {
383
410
  }
384
411
  handleSignin(signinData) {
385
412
  if (!signinData.accessToken) return;
413
+ const exp = parseJwtExp(signinData.accessToken);
386
414
  const tokens = {
387
415
  accessToken: signinData.accessToken,
388
416
  refreshToken: signinData.refreshToken,
389
- expiresAt: Math.floor(Date.now() / 1e3) + 3600,
417
+ expiresAt: exp ?? Math.floor(Date.now() / 1e3) + 3600,
390
418
  scope: ""
391
419
  };
392
420
  this.onTokensReceived?.(tokens);
@@ -841,6 +869,8 @@ var AuthManager = class {
841
869
  guidyBridge = null;
842
870
  reactNativeAuth = null;
843
871
  tokenRefreshTimer = null;
872
+ /** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
873
+ refreshInFlight = null;
844
874
  listeners = /* @__PURE__ */ new Set();
845
875
  navigationHandlers = /* @__PURE__ */ new Set();
846
876
  notificationHandlers = /* @__PURE__ */ new Set();
@@ -1046,6 +1076,14 @@ var AuthManager = class {
1046
1076
  notifyHostRouteChange(payload) {
1047
1077
  this.iframeAuth?.notifyRouteChange(payload);
1048
1078
  }
1079
+ /**
1080
+ * Iframe/WebView mode: ask the host shell to navigate to a path/URL.
1081
+ * Posts a `navigation-request` message; the host decides how to honour it.
1082
+ * No-op outside iframe mode.
1083
+ */
1084
+ requestHostNavigation(url, options) {
1085
+ this.iframeAuth?.requestHostNavigation(url, options);
1086
+ }
1049
1087
  /**
1050
1088
  * Subscribe to host `navigation` messages. Returns an unsubscribe function.
1051
1089
  * Handlers registered before iframe mode is initialized are retained and
@@ -1241,26 +1279,41 @@ var AuthManager = class {
1241
1279
  */
1242
1280
  async getValidToken() {
1243
1281
  if (!this.tokens) return null;
1244
- const expiresAt = this.resolveExpiry(this.tokens);
1245
- if (this.autoRefreshEnabled && expiresAt) {
1282
+ if (this.autoRefreshEnabled) {
1283
+ const expiresAt = this.resolveExpiry(this.tokens);
1246
1284
  const nowSec = Math.floor(Date.now() / 1e3);
1247
- if (nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS) {
1285
+ const needsRefresh = expiresAt === null || nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS;
1286
+ if (needsRefresh) {
1248
1287
  try {
1249
- const freshToken = await this.handleTokenRefresh();
1250
- return freshToken;
1288
+ return await this.handleTokenRefresh();
1251
1289
  } catch {
1252
- return null;
1290
+ return expiresAt === null ? this.tokens.accessToken : null;
1253
1291
  }
1254
1292
  }
1255
1293
  }
1256
1294
  return this.tokens.accessToken;
1257
1295
  }
1258
1296
  /**
1259
- * Handle token refresh depending on mode.
1297
+ * Handle token refresh depending on mode, sharing a single in-flight
1298
+ * request across concurrent callers. Multiple requests crossing the expiry
1299
+ * buffer at once (e.g. a dashboard firing several queries) would otherwise
1300
+ * each trigger their own refresh; with refresh-token rotation only the first
1301
+ * succeeds and the rest can leave us holding an expired token — exactly the
1302
+ * backend "JWTExpired" the proactive path is meant to prevent.
1303
+ */
1304
+ handleTokenRefresh() {
1305
+ if (this.refreshInFlight) return this.refreshInFlight;
1306
+ this.refreshInFlight = this.performTokenRefresh().finally(() => {
1307
+ this.refreshInFlight = null;
1308
+ });
1309
+ return this.refreshInFlight;
1310
+ }
1311
+ /**
1312
+ * Refresh depending on mode.
1260
1313
  * Standalone: use OAuth2Client.getValidAccessToken() which auto-refreshes.
1261
1314
  * Iframe: send postMessage to host requesting new tokens.
1262
1315
  */
1263
- async handleTokenRefresh() {
1316
+ async performTokenRefresh() {
1264
1317
  if (this.mode === "standalone" && this.standaloneAuth) {
1265
1318
  const newTokens = await this.standaloneAuth.refreshToken();
1266
1319
  this.tokens = newTokens;
@@ -1321,17 +1374,7 @@ var AuthManager = class {
1321
1374
  */
1322
1375
  resolveExpiry(tokens) {
1323
1376
  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;
1377
+ return parseJwtExp(tokens.accessToken);
1335
1378
  }
1336
1379
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1337
1380
  destroy() {
@@ -1531,6 +1574,11 @@ function DocyrusAuthProvider({
1531
1574
  managerRef.current?.notifyHostRouteChange(payload);
1532
1575
  };
1533
1576
  }, []);
1577
+ const requestHostNavigation = useMemo(() => {
1578
+ return (url, options) => {
1579
+ managerRef.current?.requestHostNavigation(url, options);
1580
+ };
1581
+ }, []);
1534
1582
  const setGuidyRoutes = useMemo(() => {
1535
1583
  return (routes) => {
1536
1584
  managerRef.current?.setGuidyRoutes(routes);
@@ -1553,6 +1601,7 @@ function DocyrusAuthProvider({
1553
1601
  onHostNotification,
1554
1602
  enableHostRouteSync,
1555
1603
  notifyRouteChange,
1604
+ requestHostNavigation,
1556
1605
  setGuidyRoutes,
1557
1606
  error
1558
1607
  }), [
@@ -1572,6 +1621,7 @@ function DocyrusAuthProvider({
1572
1621
  onHostNotification,
1573
1622
  enableHostRouteSync,
1574
1623
  notifyRouteChange,
1624
+ requestHostNavigation,
1575
1625
  setGuidyRoutes,
1576
1626
  error
1577
1627
  ]);
@@ -1618,6 +1668,10 @@ function useDocyrusHostNotification(handler) {
1618
1668
  });
1619
1669
  }, [onHostNotification]);
1620
1670
  }
1671
+ function useDocyrusRequestHostNavigation() {
1672
+ const { requestHostNavigation } = useDocyrusAuth();
1673
+ return requestHostNavigation;
1674
+ }
1621
1675
  function useDocyrusGuidyRoutes(routes) {
1622
1676
  const { setGuidyRoutes } = useDocyrusAuth();
1623
1677
  useEffect(() => {
@@ -1708,6 +1762,6 @@ function SignInButton({
1708
1762
  );
1709
1763
  }
1710
1764
 
1711
- 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 };
1765
+ 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, useDocyrusRequestHostNavigation };
1712
1766
  //# sourceMappingURL=index.js.map
1713
1767
  //# sourceMappingURL=index.js.map