@docyrus/signin 0.9.1 → 0.11.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.
@@ -242,6 +242,13 @@ var IframeAuth = class {
242
242
  allowedPattern;
243
243
  isWebView;
244
244
  onTokensReceived = null;
245
+ navigationHandlers = /* @__PURE__ */ new Set();
246
+ notificationHandlers = /* @__PURE__ */ new Set();
247
+ routeSyncEnabled = false;
248
+ originalPushState = null;
249
+ originalReplaceState = null;
250
+ routeChangeListener = null;
251
+ stopped = false;
245
252
  refreshPromise = null;
246
253
  messageHandler = null;
247
254
  constructor(allowedOrigins, allowedPattern) {
@@ -261,11 +268,104 @@ var IframeAuth = class {
261
268
  }
262
269
  /** Stop listening for postMessage events. */
263
270
  stop() {
271
+ this.stopped = true;
264
272
  if (this.messageHandler) {
265
273
  window.removeEventListener("message", this.messageHandler);
266
274
  this.messageHandler = null;
267
275
  }
268
276
  this.rejectPendingRefresh("IframeAuth stopped");
277
+ this.navigationHandlers.clear();
278
+ this.notificationHandlers.clear();
279
+ this.teardownRouteSync();
280
+ }
281
+ /**
282
+ * Patch history methods + listen for popstate/hashchange so every
283
+ * route change posts a `route-change` message to the host.
284
+ * Idempotent. Fires an immediate message with the current route.
285
+ */
286
+ enableRouteSync() {
287
+ if (this.routeSyncEnabled || typeof window === "undefined") return;
288
+ this.routeSyncEnabled = true;
289
+ const { history } = window;
290
+ this.originalPushState = history.pushState.bind(history);
291
+ this.originalReplaceState = history.replaceState.bind(history);
292
+ history.pushState = (...args) => {
293
+ const result = this.originalPushState?.(...args);
294
+ this.notifyRouteChange();
295
+ return result;
296
+ };
297
+ history.replaceState = (...args) => {
298
+ const result = this.originalReplaceState?.(...args);
299
+ this.notifyRouteChange();
300
+ return result;
301
+ };
302
+ this.routeChangeListener = () => this.notifyRouteChange();
303
+ window.addEventListener("popstate", this.routeChangeListener);
304
+ window.addEventListener("hashchange", this.routeChangeListener);
305
+ this.notifyRouteChange();
306
+ }
307
+ /**
308
+ * Post a single `route-change` message. With no argument, reads
309
+ * `window.location`. Pass a payload from a router subscription to
310
+ * report a known route instead.
311
+ */
312
+ notifyRouteChange(payload) {
313
+ if (this.stopped) return;
314
+ let path;
315
+ let search;
316
+ let hash;
317
+ if (payload) {
318
+ path = payload.path ?? "";
319
+ search = payload.search ?? "";
320
+ hash = payload.hash ?? "";
321
+ } else if (typeof window !== "undefined") {
322
+ ({ pathname: path, search, hash } = window.location);
323
+ } else {
324
+ return;
325
+ }
326
+ postToHost({
327
+ type: "route-change",
328
+ path,
329
+ search,
330
+ hash,
331
+ url: `${path}${search}${hash}`
332
+ });
333
+ }
334
+ teardownRouteSync() {
335
+ if (!this.routeSyncEnabled) return;
336
+ if (typeof window !== "undefined") {
337
+ const { history } = window;
338
+ if (this.originalPushState) history.pushState = this.originalPushState;
339
+ if (this.originalReplaceState) history.replaceState = this.originalReplaceState;
340
+ if (this.routeChangeListener) {
341
+ window.removeEventListener("popstate", this.routeChangeListener);
342
+ window.removeEventListener("hashchange", this.routeChangeListener);
343
+ }
344
+ }
345
+ this.originalPushState = null;
346
+ this.originalReplaceState = null;
347
+ this.routeChangeListener = null;
348
+ this.routeSyncEnabled = false;
349
+ }
350
+ /**
351
+ * Subscribe to host navigation messages.
352
+ * Returns an unsubscribe function.
353
+ */
354
+ onNavigation(handler) {
355
+ this.navigationHandlers.add(handler);
356
+ return () => {
357
+ this.navigationHandlers.delete(handler);
358
+ };
359
+ }
360
+ /**
361
+ * Subscribe to host notification messages.
362
+ * Returns an unsubscribe function.
363
+ */
364
+ onNotification(handler) {
365
+ this.notificationHandlers.add(handler);
366
+ return () => {
367
+ this.notificationHandlers.delete(handler);
368
+ };
269
369
  }
270
370
  /**
271
371
  * Request fresh tokens from the host.
@@ -308,8 +408,20 @@ var IframeAuth = class {
308
408
  handleMessage(ev) {
309
409
  if (!this.isOriginAllowed(ev.origin)) return;
310
410
  const data = ev.data;
311
- if (!data || typeof data !== "object" || data.type !== "signin") return;
312
- const signinData = data;
411
+ if (!data || typeof data !== "object" || typeof data.type !== "string") return;
412
+ if (data.type === "signin") {
413
+ this.handleSignin(data);
414
+ return;
415
+ }
416
+ if (data.type === "navigation") {
417
+ this.handleNavigation(data);
418
+ return;
419
+ }
420
+ if (data.type === "notification") {
421
+ this.handleNotification(data);
422
+ }
423
+ }
424
+ handleSignin(signinData) {
313
425
  if (!signinData.accessToken) return;
314
426
  const tokens = {
315
427
  accessToken: signinData.accessToken,
@@ -324,6 +436,29 @@ var IframeAuth = class {
324
436
  this.refreshPromise = null;
325
437
  }
326
438
  }
439
+ handleNavigation(message) {
440
+ if (typeof message.url !== "string" || message.url.length === 0) return;
441
+ for (const handler of this.navigationHandlers) {
442
+ try {
443
+ handler(message);
444
+ } catch (err) {
445
+ console.warn("[docyrus-signin] host navigation handler threw:", err);
446
+ }
447
+ }
448
+ }
449
+ handleNotification(message) {
450
+ const notification = message.notification;
451
+ if (!notification || typeof notification !== "object" || typeof notification.id !== "string") {
452
+ return;
453
+ }
454
+ for (const handler of this.notificationHandlers) {
455
+ try {
456
+ handler(notification);
457
+ } catch (err) {
458
+ console.warn("[docyrus-signin] host notification handler threw:", err);
459
+ }
460
+ }
461
+ }
327
462
  rejectPendingRefresh(reason) {
328
463
  if (this.refreshPromise) {
329
464
  clearTimeout(this.refreshPromise.timeoutId);
@@ -420,6 +555,8 @@ var AuthManager = class {
420
555
  reactNativeAuth = null;
421
556
  tokenRefreshTimer = null;
422
557
  listeners = /* @__PURE__ */ new Set();
558
+ navigationHandlers = /* @__PURE__ */ new Set();
559
+ notificationHandlers = /* @__PURE__ */ new Set();
423
560
  config;
424
561
  constructor(config = {}) {
425
562
  this.config = config;
@@ -558,6 +695,66 @@ var AuthManager = class {
558
695
  this.iframeAuth.start((tokens) => {
559
696
  this.setAuthenticated(tokens);
560
697
  });
698
+ this.iframeAuth.onNavigation((message) => {
699
+ for (const handler of this.navigationHandlers) {
700
+ try {
701
+ handler(message);
702
+ } catch (err) {
703
+ console.warn("[docyrus-signin] host navigation handler threw:", err);
704
+ }
705
+ }
706
+ });
707
+ this.iframeAuth.onNotification((notification) => {
708
+ for (const handler of this.notificationHandlers) {
709
+ try {
710
+ handler(notification);
711
+ } catch (err) {
712
+ console.warn("[docyrus-signin] host notification handler threw:", err);
713
+ }
714
+ }
715
+ });
716
+ if (this.config.syncRouteToHost) {
717
+ this.iframeAuth.enableRouteSync();
718
+ }
719
+ }
720
+ /**
721
+ * Iframe/WebView mode: start auto-syncing the embedded app's route
722
+ * to the host. Patches history methods and listens for popstate /
723
+ * hashchange. Idempotent. No-op outside iframe mode.
724
+ */
725
+ enableHostRouteSync() {
726
+ this.iframeAuth?.enableRouteSync();
727
+ }
728
+ /**
729
+ * Iframe/WebView mode: post a single `route-change` message to the
730
+ * host. With no argument, reads `window.location`. Pass a payload
731
+ * from a router subscription to report a known route. No-op outside
732
+ * iframe mode.
733
+ */
734
+ notifyHostRouteChange(payload) {
735
+ this.iframeAuth?.notifyRouteChange(payload);
736
+ }
737
+ /**
738
+ * Subscribe to host `navigation` messages. Returns an unsubscribe function.
739
+ * Handlers registered before iframe mode is initialized are retained and
740
+ * will fire once messages arrive. No-op in non-iframe modes.
741
+ */
742
+ onHostNavigation(handler) {
743
+ this.navigationHandlers.add(handler);
744
+ return () => {
745
+ this.navigationHandlers.delete(handler);
746
+ };
747
+ }
748
+ /**
749
+ * Subscribe to host `notification` messages. Returns an unsubscribe function.
750
+ * Handlers registered before iframe mode is initialized are retained and
751
+ * will fire once messages arrive. No-op in non-iframe modes.
752
+ */
753
+ onHostNotification(handler) {
754
+ this.notificationHandlers.add(handler);
755
+ return () => {
756
+ this.notificationHandlers.delete(handler);
757
+ };
561
758
  }
562
759
  /**
563
760
  * React Native mode initialization.
@@ -825,6 +1022,8 @@ var AuthManager = class {
825
1022
  this.clearTokenRefreshTimer();
826
1023
  if (this.iframeAuth) this.iframeAuth.stop();
827
1024
  this.listeners.clear();
1025
+ this.navigationHandlers.clear();
1026
+ this.notificationHandlers.clear();
828
1027
  }
829
1028
  };
830
1029
 
@@ -990,6 +1189,28 @@ function DocyrusAuthProvider({
990
1189
  await managerRef.current?.refreshUser();
991
1190
  };
992
1191
  }, []);
1192
+ const onHostNavigation = useMemo(() => {
1193
+ return (handler) => {
1194
+ return managerRef.current?.onHostNavigation(handler) ?? (() => {
1195
+ });
1196
+ };
1197
+ }, []);
1198
+ const onHostNotification = useMemo(() => {
1199
+ return (handler) => {
1200
+ return managerRef.current?.onHostNotification(handler) ?? (() => {
1201
+ });
1202
+ };
1203
+ }, []);
1204
+ const enableHostRouteSync = useMemo(() => {
1205
+ return () => {
1206
+ managerRef.current?.enableHostRouteSync();
1207
+ };
1208
+ }, []);
1209
+ const notifyRouteChange = useMemo(() => {
1210
+ return (payload) => {
1211
+ managerRef.current?.notifyHostRouteChange(payload);
1212
+ };
1213
+ }, []);
993
1214
  const value = useMemo(() => ({
994
1215
  status,
995
1216
  mode,
@@ -1003,6 +1224,10 @@ function DocyrusAuthProvider({
1003
1224
  hasRole: hasRole2,
1004
1225
  hasPermission: hasPermission2,
1005
1226
  refreshUser,
1227
+ onHostNavigation,
1228
+ onHostNotification,
1229
+ enableHostRouteSync,
1230
+ notifyRouteChange,
1006
1231
  error
1007
1232
  }), [
1008
1233
  status,
@@ -1017,6 +1242,10 @@ function DocyrusAuthProvider({
1017
1242
  hasRole2,
1018
1243
  hasPermission2,
1019
1244
  refreshUser,
1245
+ onHostNavigation,
1246
+ onHostNotification,
1247
+ enableHostRouteSync,
1248
+ notifyRouteChange,
1020
1249
  error
1021
1250
  ]);
1022
1251
  return /* @__PURE__ */ jsx(DocyrusAuthContext, { value, children });