@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.
package/dist/vue/index.js CHANGED
@@ -163,6 +163,13 @@ var IframeAuth = class {
163
163
  allowedPattern;
164
164
  isWebView;
165
165
  onTokensReceived = null;
166
+ navigationHandlers = /* @__PURE__ */ new Set();
167
+ notificationHandlers = /* @__PURE__ */ new Set();
168
+ routeSyncEnabled = false;
169
+ originalPushState = null;
170
+ originalReplaceState = null;
171
+ routeChangeListener = null;
172
+ stopped = false;
166
173
  refreshPromise = null;
167
174
  messageHandler = null;
168
175
  constructor(allowedOrigins, allowedPattern) {
@@ -182,11 +189,104 @@ var IframeAuth = class {
182
189
  }
183
190
  /** Stop listening for postMessage events. */
184
191
  stop() {
192
+ this.stopped = true;
185
193
  if (this.messageHandler) {
186
194
  window.removeEventListener("message", this.messageHandler);
187
195
  this.messageHandler = null;
188
196
  }
189
197
  this.rejectPendingRefresh("IframeAuth stopped");
198
+ this.navigationHandlers.clear();
199
+ this.notificationHandlers.clear();
200
+ this.teardownRouteSync();
201
+ }
202
+ /**
203
+ * Patch history methods + listen for popstate/hashchange so every
204
+ * route change posts a `route-change` message to the host.
205
+ * Idempotent. Fires an immediate message with the current route.
206
+ */
207
+ enableRouteSync() {
208
+ if (this.routeSyncEnabled || typeof window === "undefined") return;
209
+ this.routeSyncEnabled = true;
210
+ const { history } = window;
211
+ this.originalPushState = history.pushState.bind(history);
212
+ this.originalReplaceState = history.replaceState.bind(history);
213
+ history.pushState = (...args) => {
214
+ const result = this.originalPushState?.(...args);
215
+ this.notifyRouteChange();
216
+ return result;
217
+ };
218
+ history.replaceState = (...args) => {
219
+ const result = this.originalReplaceState?.(...args);
220
+ this.notifyRouteChange();
221
+ return result;
222
+ };
223
+ this.routeChangeListener = () => this.notifyRouteChange();
224
+ window.addEventListener("popstate", this.routeChangeListener);
225
+ window.addEventListener("hashchange", this.routeChangeListener);
226
+ this.notifyRouteChange();
227
+ }
228
+ /**
229
+ * Post a single `route-change` message. With no argument, reads
230
+ * `window.location`. Pass a payload from a router subscription to
231
+ * report a known route instead.
232
+ */
233
+ notifyRouteChange(payload) {
234
+ if (this.stopped) return;
235
+ let path;
236
+ let search;
237
+ let hash;
238
+ if (payload) {
239
+ path = payload.path ?? "";
240
+ search = payload.search ?? "";
241
+ hash = payload.hash ?? "";
242
+ } else if (typeof window !== "undefined") {
243
+ ({ pathname: path, search, hash } = window.location);
244
+ } else {
245
+ return;
246
+ }
247
+ postToHost({
248
+ type: "route-change",
249
+ path,
250
+ search,
251
+ hash,
252
+ url: `${path}${search}${hash}`
253
+ });
254
+ }
255
+ teardownRouteSync() {
256
+ if (!this.routeSyncEnabled) return;
257
+ if (typeof window !== "undefined") {
258
+ const { history } = window;
259
+ if (this.originalPushState) history.pushState = this.originalPushState;
260
+ if (this.originalReplaceState) history.replaceState = this.originalReplaceState;
261
+ if (this.routeChangeListener) {
262
+ window.removeEventListener("popstate", this.routeChangeListener);
263
+ window.removeEventListener("hashchange", this.routeChangeListener);
264
+ }
265
+ }
266
+ this.originalPushState = null;
267
+ this.originalReplaceState = null;
268
+ this.routeChangeListener = null;
269
+ this.routeSyncEnabled = false;
270
+ }
271
+ /**
272
+ * Subscribe to host navigation messages.
273
+ * Returns an unsubscribe function.
274
+ */
275
+ onNavigation(handler) {
276
+ this.navigationHandlers.add(handler);
277
+ return () => {
278
+ this.navigationHandlers.delete(handler);
279
+ };
280
+ }
281
+ /**
282
+ * Subscribe to host notification messages.
283
+ * Returns an unsubscribe function.
284
+ */
285
+ onNotification(handler) {
286
+ this.notificationHandlers.add(handler);
287
+ return () => {
288
+ this.notificationHandlers.delete(handler);
289
+ };
190
290
  }
191
291
  /**
192
292
  * Request fresh tokens from the host.
@@ -229,8 +329,20 @@ var IframeAuth = class {
229
329
  handleMessage(ev) {
230
330
  if (!this.isOriginAllowed(ev.origin)) return;
231
331
  const data = ev.data;
232
- if (!data || typeof data !== "object" || data.type !== "signin") return;
233
- const signinData = data;
332
+ if (!data || typeof data !== "object" || typeof data.type !== "string") return;
333
+ if (data.type === "signin") {
334
+ this.handleSignin(data);
335
+ return;
336
+ }
337
+ if (data.type === "navigation") {
338
+ this.handleNavigation(data);
339
+ return;
340
+ }
341
+ if (data.type === "notification") {
342
+ this.handleNotification(data);
343
+ }
344
+ }
345
+ handleSignin(signinData) {
234
346
  if (!signinData.accessToken) return;
235
347
  const tokens = {
236
348
  accessToken: signinData.accessToken,
@@ -245,6 +357,29 @@ var IframeAuth = class {
245
357
  this.refreshPromise = null;
246
358
  }
247
359
  }
360
+ handleNavigation(message) {
361
+ if (typeof message.url !== "string" || message.url.length === 0) return;
362
+ for (const handler of this.navigationHandlers) {
363
+ try {
364
+ handler(message);
365
+ } catch (err) {
366
+ console.warn("[docyrus-signin] host navigation handler threw:", err);
367
+ }
368
+ }
369
+ }
370
+ handleNotification(message) {
371
+ const notification = message.notification;
372
+ if (!notification || typeof notification !== "object" || typeof notification.id !== "string") {
373
+ return;
374
+ }
375
+ for (const handler of this.notificationHandlers) {
376
+ try {
377
+ handler(notification);
378
+ } catch (err) {
379
+ console.warn("[docyrus-signin] host notification handler threw:", err);
380
+ }
381
+ }
382
+ }
248
383
  rejectPendingRefresh(reason) {
249
384
  if (this.refreshPromise) {
250
385
  clearTimeout(this.refreshPromise.timeoutId);
@@ -417,6 +552,8 @@ var AuthManager = class {
417
552
  reactNativeAuth = null;
418
553
  tokenRefreshTimer = null;
419
554
  listeners = /* @__PURE__ */ new Set();
555
+ navigationHandlers = /* @__PURE__ */ new Set();
556
+ notificationHandlers = /* @__PURE__ */ new Set();
420
557
  config;
421
558
  constructor(config = {}) {
422
559
  this.config = config;
@@ -555,6 +692,66 @@ var AuthManager = class {
555
692
  this.iframeAuth.start((tokens) => {
556
693
  this.setAuthenticated(tokens);
557
694
  });
695
+ this.iframeAuth.onNavigation((message) => {
696
+ for (const handler of this.navigationHandlers) {
697
+ try {
698
+ handler(message);
699
+ } catch (err) {
700
+ console.warn("[docyrus-signin] host navigation handler threw:", err);
701
+ }
702
+ }
703
+ });
704
+ this.iframeAuth.onNotification((notification) => {
705
+ for (const handler of this.notificationHandlers) {
706
+ try {
707
+ handler(notification);
708
+ } catch (err) {
709
+ console.warn("[docyrus-signin] host notification handler threw:", err);
710
+ }
711
+ }
712
+ });
713
+ if (this.config.syncRouteToHost) {
714
+ this.iframeAuth.enableRouteSync();
715
+ }
716
+ }
717
+ /**
718
+ * Iframe/WebView mode: start auto-syncing the embedded app's route
719
+ * to the host. Patches history methods and listens for popstate /
720
+ * hashchange. Idempotent. No-op outside iframe mode.
721
+ */
722
+ enableHostRouteSync() {
723
+ this.iframeAuth?.enableRouteSync();
724
+ }
725
+ /**
726
+ * Iframe/WebView mode: post a single `route-change` message to the
727
+ * host. With no argument, reads `window.location`. Pass a payload
728
+ * from a router subscription to report a known route. No-op outside
729
+ * iframe mode.
730
+ */
731
+ notifyHostRouteChange(payload) {
732
+ this.iframeAuth?.notifyRouteChange(payload);
733
+ }
734
+ /**
735
+ * Subscribe to host `navigation` messages. Returns an unsubscribe function.
736
+ * Handlers registered before iframe mode is initialized are retained and
737
+ * will fire once messages arrive. No-op in non-iframe modes.
738
+ */
739
+ onHostNavigation(handler) {
740
+ this.navigationHandlers.add(handler);
741
+ return () => {
742
+ this.navigationHandlers.delete(handler);
743
+ };
744
+ }
745
+ /**
746
+ * Subscribe to host `notification` messages. Returns an unsubscribe function.
747
+ * Handlers registered before iframe mode is initialized are retained and
748
+ * will fire once messages arrive. No-op in non-iframe modes.
749
+ */
750
+ onHostNotification(handler) {
751
+ this.notificationHandlers.add(handler);
752
+ return () => {
753
+ this.notificationHandlers.delete(handler);
754
+ };
558
755
  }
559
756
  /**
560
757
  * React Native mode initialization.
@@ -822,6 +1019,8 @@ var AuthManager = class {
822
1019
  this.clearTokenRefreshTimer();
823
1020
  if (this.iframeAuth) this.iframeAuth.stop();
824
1021
  this.listeners.clear();
1022
+ this.navigationHandlers.clear();
1023
+ this.notificationHandlers.clear();
825
1024
  }
826
1025
  };
827
1026
 
@@ -854,7 +1053,8 @@ var DocyrusAuthProvider = defineComponent({
854
1053
  storageKeyPrefix: { type: String, default: void 0 },
855
1054
  tokenStorage: { type: Object, default: void 0 },
856
1055
  forceMode: { type: String, default: void 0 },
857
- initialTokens: { type: Object, default: void 0 }
1056
+ initialTokens: { type: Object, default: void 0 },
1057
+ syncRouteToHost: { type: Boolean, default: false }
858
1058
  },
859
1059
  setup(props, { slots }) {
860
1060
  const status = ref("loading");
@@ -875,6 +1075,20 @@ var DocyrusAuthProvider = defineComponent({
875
1075
  const getAuthorizationUrl = async () => {
876
1076
  return manager?.getAuthorizationUrl() ?? null;
877
1077
  };
1078
+ const onHostNavigation = (handler) => {
1079
+ return manager?.onHostNavigation(handler) ?? (() => {
1080
+ });
1081
+ };
1082
+ const onHostNotification = (handler) => {
1083
+ return manager?.onHostNotification(handler) ?? (() => {
1084
+ });
1085
+ };
1086
+ const enableHostRouteSync = () => {
1087
+ manager?.enableHostRouteSync();
1088
+ };
1089
+ const notifyRouteChange = (payload) => {
1090
+ manager?.notifyHostRouteChange(payload);
1091
+ };
878
1092
  const authState = {
879
1093
  get status() {
880
1094
  return status.value;
@@ -894,7 +1108,11 @@ var DocyrusAuthProvider = defineComponent({
894
1108
  signIn,
895
1109
  signInWithTokens,
896
1110
  getAuthorizationUrl,
897
- signOut
1111
+ signOut,
1112
+ onHostNavigation,
1113
+ onHostNotification,
1114
+ enableHostRouteSync,
1115
+ notifyRouteChange
898
1116
  };
899
1117
  provide(DOCYRUS_AUTH_KEY, authState);
900
1118
  onMounted(() => {
@@ -908,7 +1126,8 @@ var DocyrusAuthProvider = defineComponent({
908
1126
  storageKeyPrefix: props.storageKeyPrefix,
909
1127
  tokenStorage: props.tokenStorage,
910
1128
  forceMode: props.forceMode,
911
- initialTokens: props.initialTokens
1129
+ initialTokens: props.initialTokens,
1130
+ syncRouteToHost: props.syncRouteToHost
912
1131
  };
913
1132
  manager = new AuthManager(config);
914
1133
  mode.value = manager.getMode();