@docyrus/signin 0.10.0 → 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.
@@ -54,6 +54,14 @@ interface DocyrusAuthConfig {
54
54
  ssr?: boolean;
55
55
  /** Cookie name for SSR token sync. Default: 'docyrus-token' */
56
56
  ssrCookieKey?: string;
57
+ /**
58
+ * Iframe/WebView mode: post a `route-change` message to the host on
59
+ * every history change so the host can reflect the embedded app's
60
+ * route in its address bar. Patches `history.pushState/replaceState`
61
+ * and listens for `popstate`/`hashchange`. No-op outside iframe mode.
62
+ * Default: false.
63
+ */
64
+ syncRouteToHost?: boolean;
57
65
  /** React Native: custom URI scheme redirect (e.g., 'myapp://auth/callback'). Required when forceMode is 'react-native' */
58
66
  nativeRedirectUri?: string;
59
67
  /** React Native: function to open an in-app browser auth session (e.g., WebBrowser.openAuthSessionAsync from expo-web-browser) */
@@ -97,6 +105,20 @@ interface DocyrusAuthContextValue {
97
105
  * Returns an unsubscribe function. No-op in non-iframe modes.
98
106
  */
99
107
  onHostNotification: (handler: HostNotificationHandler) => () => void;
108
+ /**
109
+ * Iframe/WebView mode: start auto-syncing the embedded app's route
110
+ * to the host. Patches `history.pushState/replaceState` and listens
111
+ * for `popstate`/`hashchange`. Safe to call multiple times. No-op in
112
+ * non-iframe modes.
113
+ */
114
+ enableHostRouteSync: () => void;
115
+ /**
116
+ * Iframe/WebView mode: post a single `route-change` message to the host.
117
+ * When called with no argument, the current `window.location` is read.
118
+ * Pass a payload to report a specific route (useful with router
119
+ * subscriptions). No-op in non-iframe modes.
120
+ */
121
+ notifyRouteChange: (payload?: RouteChangePayload) => void;
100
122
  /** Any error that occurred during auth */
101
123
  error: Error | null;
102
124
  }
@@ -110,6 +132,28 @@ interface HostSignInMessage {
110
132
  interface TokenRefreshRequestMessage {
111
133
  type: 'token-refresh-request';
112
134
  }
135
+ /**
136
+ * PostMessage payload: app reports its current route to the host.
137
+ * Sent on every history change so the host can reflect it in the
138
+ * browser address bar.
139
+ */
140
+ interface AppRouteChangeMessage {
141
+ type: 'route-change';
142
+ /** location.pathname (e.g. "/customers/123") */
143
+ path: string;
144
+ /** location.search including leading "?" — empty string if none */
145
+ search: string;
146
+ /** location.hash including leading "#" — empty string if none */
147
+ hash: string;
148
+ /** Convenience: `path + search + hash` */
149
+ url: string;
150
+ }
151
+ /** Payload for manual `notifyRouteChange` calls */
152
+ interface RouteChangePayload {
153
+ path?: string;
154
+ search?: string;
155
+ hash?: string;
156
+ }
113
157
  /** PostMessage payload: host asks the embedded app to navigate to a URL */
114
158
  interface HostNavigationMessage {
115
159
  type: 'navigation';
@@ -186,4 +230,4 @@ interface PermissionConfig {
186
230
  alwaysPermittedDataSources?: Record<string, AclOperation[]>;
187
231
  }
188
232
 
189
- export type { AclOperation as A, DocyrusAuthConfig as D, HostNavigationHandler as H, OpenAuthSessionFn as O, PermissionConfig as P, TokenRefreshRequestMessage as T, AuthMode as a, AuthSessionResult as b, AuthStatus as c, DocyrusAclRule as d, DocyrusAuthContextValue as e, DocyrusRole as f, DocyrusUser as g, HostNotificationHandler as h, DocyrusNotification as i, HostNavigationMessage as j, HostNotificationMessage as k, HostSignInMessage as l };
233
+ export type { AclOperation as A, DocyrusAuthConfig as D, HostNavigationHandler as H, OpenAuthSessionFn as O, PermissionConfig as P, RouteChangePayload as R, TokenRefreshRequestMessage as T, AuthMode as a, AuthSessionResult as b, AuthStatus as c, DocyrusAclRule as d, DocyrusAuthContextValue as e, DocyrusRole as f, DocyrusUser as g, HostNotificationHandler as h, AppRouteChangeMessage as i, DocyrusNotification as j, HostNavigationMessage as k, HostNotificationMessage as l, HostSignInMessage as m };
@@ -1,6 +1,6 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode } from 'react';
3
- import { D as DocyrusAuthConfig, e as DocyrusAuthContextValue } from './types-vd0OGryp.js';
3
+ import { D as DocyrusAuthConfig, e as DocyrusAuthContextValue } from './types-DjG8tL1X.js';
4
4
  import { RestApiClient } from '@docyrus/api-client';
5
5
 
6
6
  interface DocyrusAuthProviderProps extends DocyrusAuthConfig {
@@ -1,8 +1,8 @@
1
1
  import * as vue from 'vue';
2
2
  import { PropType } from 'vue';
3
3
  import { OAuth2TokenStorage, OAuth2Tokens, RestApiClient } from '@docyrus/api-client';
4
- import { a as AuthMode, c as AuthStatus, H as HostNavigationHandler, h as HostNotificationHandler } from '../types-vd0OGryp.js';
5
- export { D as DocyrusAuthConfig } from '../types-vd0OGryp.js';
4
+ import { a as AuthMode, c as AuthStatus, H as HostNavigationHandler, h as HostNotificationHandler, R as RouteChangePayload } from '../types-DjG8tL1X.js';
5
+ export { D as DocyrusAuthConfig } from '../types-DjG8tL1X.js';
6
6
 
7
7
  interface DocyrusAuthState {
8
8
  status: AuthStatus;
@@ -16,6 +16,8 @@ interface DocyrusAuthState {
16
16
  signOut: () => Promise<void>;
17
17
  onHostNavigation: (handler: HostNavigationHandler) => () => void;
18
18
  onHostNotification: (handler: HostNotificationHandler) => () => void;
19
+ enableHostRouteSync: () => void;
20
+ notifyRouteChange: (payload?: RouteChangePayload) => void;
19
21
  }
20
22
  /**
21
23
  * Vue composable to access Docyrus auth state.
@@ -90,6 +92,10 @@ declare const DocyrusAuthProvider: vue.DefineComponent<vue.ExtractPropTypes<{
90
92
  type: PropType<OAuth2Tokens>;
91
93
  default: undefined;
92
94
  };
95
+ syncRouteToHost: {
96
+ type: BooleanConstructor;
97
+ default: boolean;
98
+ };
93
99
  }>, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
94
100
  [key: string]: any;
95
101
  }>[] | undefined, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
@@ -133,6 +139,10 @@ declare const DocyrusAuthProvider: vue.DefineComponent<vue.ExtractPropTypes<{
133
139
  type: PropType<OAuth2Tokens>;
134
140
  default: undefined;
135
141
  };
142
+ syncRouteToHost: {
143
+ type: BooleanConstructor;
144
+ default: boolean;
145
+ };
136
146
  }>> & Readonly<{}>, {
137
147
  clientId: string;
138
148
  apiUrl: string;
@@ -144,6 +154,7 @@ declare const DocyrusAuthProvider: vue.DefineComponent<vue.ExtractPropTypes<{
144
154
  tokenStorage: OAuth2TokenStorage;
145
155
  forceMode: AuthMode;
146
156
  initialTokens: OAuth2Tokens;
157
+ syncRouteToHost: boolean;
147
158
  }, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
148
159
  /**
149
160
  * "Sign in with Docyrus" button component.
package/dist/vue/index.js CHANGED
@@ -165,6 +165,11 @@ var IframeAuth = class {
165
165
  onTokensReceived = null;
166
166
  navigationHandlers = /* @__PURE__ */ new Set();
167
167
  notificationHandlers = /* @__PURE__ */ new Set();
168
+ routeSyncEnabled = false;
169
+ originalPushState = null;
170
+ originalReplaceState = null;
171
+ routeChangeListener = null;
172
+ stopped = false;
168
173
  refreshPromise = null;
169
174
  messageHandler = null;
170
175
  constructor(allowedOrigins, allowedPattern) {
@@ -184,6 +189,7 @@ var IframeAuth = class {
184
189
  }
185
190
  /** Stop listening for postMessage events. */
186
191
  stop() {
192
+ this.stopped = true;
187
193
  if (this.messageHandler) {
188
194
  window.removeEventListener("message", this.messageHandler);
189
195
  this.messageHandler = null;
@@ -191,6 +197,76 @@ var IframeAuth = class {
191
197
  this.rejectPendingRefresh("IframeAuth stopped");
192
198
  this.navigationHandlers.clear();
193
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;
194
270
  }
195
271
  /**
196
272
  * Subscribe to host navigation messages.
@@ -634,6 +710,26 @@ var AuthManager = class {
634
710
  }
635
711
  }
636
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);
637
733
  }
638
734
  /**
639
735
  * Subscribe to host `navigation` messages. Returns an unsubscribe function.
@@ -957,7 +1053,8 @@ var DocyrusAuthProvider = defineComponent({
957
1053
  storageKeyPrefix: { type: String, default: void 0 },
958
1054
  tokenStorage: { type: Object, default: void 0 },
959
1055
  forceMode: { type: String, default: void 0 },
960
- initialTokens: { type: Object, default: void 0 }
1056
+ initialTokens: { type: Object, default: void 0 },
1057
+ syncRouteToHost: { type: Boolean, default: false }
961
1058
  },
962
1059
  setup(props, { slots }) {
963
1060
  const status = ref("loading");
@@ -986,6 +1083,12 @@ var DocyrusAuthProvider = defineComponent({
986
1083
  return manager?.onHostNotification(handler) ?? (() => {
987
1084
  });
988
1085
  };
1086
+ const enableHostRouteSync = () => {
1087
+ manager?.enableHostRouteSync();
1088
+ };
1089
+ const notifyRouteChange = (payload) => {
1090
+ manager?.notifyHostRouteChange(payload);
1091
+ };
989
1092
  const authState = {
990
1093
  get status() {
991
1094
  return status.value;
@@ -1007,7 +1110,9 @@ var DocyrusAuthProvider = defineComponent({
1007
1110
  getAuthorizationUrl,
1008
1111
  signOut,
1009
1112
  onHostNavigation,
1010
- onHostNotification
1113
+ onHostNotification,
1114
+ enableHostRouteSync,
1115
+ notifyRouteChange
1011
1116
  };
1012
1117
  provide(DOCYRUS_AUTH_KEY, authState);
1013
1118
  onMounted(() => {
@@ -1021,7 +1126,8 @@ var DocyrusAuthProvider = defineComponent({
1021
1126
  storageKeyPrefix: props.storageKeyPrefix,
1022
1127
  tokenStorage: props.tokenStorage,
1023
1128
  forceMode: props.forceMode,
1024
- initialTokens: props.initialTokens
1129
+ initialTokens: props.initialTokens,
1130
+ syncRouteToHost: props.syncRouteToHost
1025
1131
  };
1026
1132
  manager = new AuthManager(config);
1027
1133
  mode.value = manager.getMode();