@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/README.md +152 -0
- package/dist/{constants-vWEY0rfK.d.ts → constants-DuNDcs9I.d.ts} +28 -1
- package/dist/core-index.d.ts +40 -4
- package/dist/core-index.js +201 -2
- package/dist/core-index.js.map +1 -1
- package/dist/index.d.ts +27 -7
- package/dist/index.js +258 -3
- package/dist/index.js.map +1 -1
- package/dist/react-native/index.d.ts +4 -5
- package/dist/react-native/index.js +231 -2
- package/dist/react-native/index.js.map +1 -1
- package/dist/{types-CmD-7GqC.d.ts → types-DjG8tL1X.d.ts} +90 -1
- package/dist/{use-docyrus-client-BgaKblAo.d.ts → use-docyrus-client-CQ0J_msv.d.ts} +2 -3
- package/dist/vue/index.d.ts +15 -2
- package/dist/vue/index.js +224 -5
- package/dist/vue/index.js.map +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -9,6 +9,8 @@ Supports **React**, **Vue**, **React Native**, **Electron**, and **Next.js SSR**
|
|
|
9
9
|
- **Auto Environment Detection**: Detects standalone vs iframe mode automatically
|
|
10
10
|
- **OAuth2 PKCE**: Full Authorization Code flow with PKCE (S256) for standalone apps
|
|
11
11
|
- **Iframe Auth**: Receives tokens via `postMessage` from `*.docyrus.app` hosts
|
|
12
|
+
- **Host Message Hooks**: Subscribe to `navigation` and `notification` messages pushed by the host into the embedded app
|
|
13
|
+
- **Route Sync**: Mirror the embedded app's route to the host so the host can reflect it in its address bar
|
|
12
14
|
- **Token Auto-Refresh**: Proactive token refresh before expiry in both modes (opt out with `autoRefresh: false` when an external system owns the token lifecycle)
|
|
13
15
|
- **Token Bootstrap**: Start a session from existing access/refresh tokens (e.g. device flow handoff)
|
|
14
16
|
- **React Hooks**: `useDocyrusAuth()` and `useDocyrusClient()` for easy integration
|
|
@@ -251,6 +253,7 @@ Pass props to `DocyrusAuthProvider` to override defaults:
|
|
|
251
253
|
| `allowedHostOrigins` | `string[]` | `undefined` | Extra trusted iframe origins |
|
|
252
254
|
| `ssr` | `boolean` | `false` | Sync access token to cookie for server components |
|
|
253
255
|
| `ssrCookieKey` | `string` | `docyrus-token` | Cookie name for SSR token sync |
|
|
256
|
+
| `syncRouteToHost` | `boolean` | `false` | Iframe mode: post `route-change` to the host on every history change |
|
|
254
257
|
|
|
255
258
|
## Hooks / Composables
|
|
256
259
|
|
|
@@ -272,10 +275,37 @@ const {
|
|
|
272
275
|
hasRole, // (role) => boolean — check if user has a role by slug or uid
|
|
273
276
|
hasPermission, // (operation, dataSourceId?) => boolean — check ACL permission
|
|
274
277
|
refreshUser, // () => Promise<void> — re-fetch user from API
|
|
278
|
+
onHostNavigation, // (handler) => () => void — subscribe to host navigation messages (iframe mode)
|
|
279
|
+
onHostNotification, // (handler) => () => void — subscribe to host notification messages (iframe mode)
|
|
280
|
+
enableHostRouteSync, // () => void — patch history + fire `route-change` on every change (iframe mode)
|
|
281
|
+
notifyRouteChange, // (payload?) => void — post a single `route-change` to the host (iframe mode)
|
|
275
282
|
error // Error | null
|
|
276
283
|
} = useDocyrusAuth();
|
|
277
284
|
```
|
|
278
285
|
|
|
286
|
+
### useDocyrusHostNavigation() / useDocyrusHostNotification()
|
|
287
|
+
|
|
288
|
+
React-only convenience hooks for subscribing to host postMessage events
|
|
289
|
+
from within an embedded iframe/WebView. Both subscribe on mount and
|
|
290
|
+
unsubscribe on unmount; the latest closure is always invoked so the
|
|
291
|
+
handler does not need to be memoized. No-op outside iframe mode.
|
|
292
|
+
|
|
293
|
+
```tsx
|
|
294
|
+
import {
|
|
295
|
+
useDocyrusHostNavigation,
|
|
296
|
+
useDocyrusHostNotification,
|
|
297
|
+
type DocyrusNotification
|
|
298
|
+
} from '@docyrus/signin';
|
|
299
|
+
|
|
300
|
+
useDocyrusHostNavigation(({ url }) => {
|
|
301
|
+
navigate(url);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
useDocyrusHostNotification((notification: DocyrusNotification) => {
|
|
305
|
+
showToast(notification.subject, notification.message);
|
|
306
|
+
});
|
|
307
|
+
```
|
|
308
|
+
|
|
279
309
|
### useDocyrusClient()
|
|
280
310
|
|
|
281
311
|
Shorthand hook/composable that returns just the API client:
|
|
@@ -403,6 +433,128 @@ Used when the app is embedded in an iframe on `*.docyrus.app`. The flow:
|
|
|
403
433
|
4. When tokens expire, provider sends `{ type: 'token-refresh-request' }` to host
|
|
404
434
|
5. Host responds with fresh tokens
|
|
405
435
|
|
|
436
|
+
In addition to auth messages, the host may push two side-channel messages
|
|
437
|
+
that the embedded app can subscribe to:
|
|
438
|
+
|
|
439
|
+
| Message | Payload | Purpose |
|
|
440
|
+
|---------|---------|---------|
|
|
441
|
+
| `{ type: 'navigation', url }` | `url: string` | Host asks the app to navigate (e.g. deep-link from a host menu) |
|
|
442
|
+
| `{ type: 'notification', notification }` | `DocyrusNotification` | Host pushes a notification to the embedded app |
|
|
443
|
+
|
|
444
|
+
**React** — register handlers with the provided hooks (latest closure is
|
|
445
|
+
always invoked, no need to memoize):
|
|
446
|
+
|
|
447
|
+
```tsx
|
|
448
|
+
import {
|
|
449
|
+
useDocyrusHostNavigation,
|
|
450
|
+
useDocyrusHostNotification
|
|
451
|
+
} from '@docyrus/signin';
|
|
452
|
+
|
|
453
|
+
function Shell() {
|
|
454
|
+
const navigate = useNavigate();
|
|
455
|
+
|
|
456
|
+
useDocyrusHostNavigation(({ url }) => {
|
|
457
|
+
navigate(url);
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
useDocyrusHostNotification((notification) => {
|
|
461
|
+
toast({
|
|
462
|
+
title: notification.subject,
|
|
463
|
+
description: notification.message
|
|
464
|
+
});
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
return <Routes />;
|
|
468
|
+
}
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
**Vue** — register handlers via the composable; both return an
|
|
472
|
+
unsubscribe function:
|
|
473
|
+
|
|
474
|
+
```vue
|
|
475
|
+
<script setup lang="ts">
|
|
476
|
+
import { onMounted, onUnmounted } from 'vue';
|
|
477
|
+
import { getDocyrusAuth } from '@docyrus/signin/vue';
|
|
478
|
+
|
|
479
|
+
const auth = getDocyrusAuth();
|
|
480
|
+
let stopNav: (() => void) | undefined;
|
|
481
|
+
let stopNote: (() => void) | undefined;
|
|
482
|
+
|
|
483
|
+
onMounted(() => {
|
|
484
|
+
stopNav = auth.onHostNavigation(({ url }) => {
|
|
485
|
+
router.push(url);
|
|
486
|
+
});
|
|
487
|
+
stopNote = auth.onHostNotification((notification) => {
|
|
488
|
+
toast.show(notification.subject, notification.message);
|
|
489
|
+
});
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
onUnmounted(() => {
|
|
493
|
+
stopNav?.();
|
|
494
|
+
stopNote?.();
|
|
495
|
+
});
|
|
496
|
+
</script>
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
Both subscriptions are no-ops outside iframe/WebView mode, so handlers
|
|
500
|
+
are safe to register unconditionally.
|
|
501
|
+
|
|
502
|
+
#### Route → Host sync (address bar reflection)
|
|
503
|
+
|
|
504
|
+
When the embedded app is inside a host shell, the shell often wants its
|
|
505
|
+
own browser address bar to reflect the app's current route (so users can
|
|
506
|
+
copy/share/bookmark URLs). The embedded app reports route changes with:
|
|
507
|
+
|
|
508
|
+
```
|
|
509
|
+
App → Host: { type: 'route-change', path, search, hash, url }
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
The simplest setup — pass `syncRouteToHost` to the provider. It patches
|
|
513
|
+
`history.pushState/replaceState` and listens for `popstate`/`hashchange`,
|
|
514
|
+
so every route change in the embedded app — regardless of router
|
|
515
|
+
(React Router, TanStack Router, Next, plain history) — posts a message:
|
|
516
|
+
|
|
517
|
+
```tsx
|
|
518
|
+
<DocyrusAuthProvider apiUrl="..." clientId="..." syncRouteToHost>
|
|
519
|
+
<App />
|
|
520
|
+
</DocyrusAuthProvider>
|
|
521
|
+
```
|
|
522
|
+
|
|
523
|
+
Or opt in scoped to a component with the hook:
|
|
524
|
+
|
|
525
|
+
```tsx
|
|
526
|
+
import { useDocyrusHostRouteSync } from '@docyrus/signin';
|
|
527
|
+
|
|
528
|
+
function Shell() {
|
|
529
|
+
useDocyrusHostRouteSync();
|
|
530
|
+
return <Routes />;
|
|
531
|
+
}
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
Or report routes manually from a router subscription (avoids history
|
|
535
|
+
patching, lets the router be the source of truth):
|
|
536
|
+
|
|
537
|
+
```tsx
|
|
538
|
+
import { useDocyrusAuth } from '@docyrus/signin';
|
|
539
|
+
|
|
540
|
+
function RouteReporter() {
|
|
541
|
+
const { notifyRouteChange } = useDocyrusAuth();
|
|
542
|
+
const location = useLocation(); // React Router / TanStack Router / etc.
|
|
543
|
+
|
|
544
|
+
useEffect(() => {
|
|
545
|
+
notifyRouteChange({
|
|
546
|
+
path: location.pathname,
|
|
547
|
+
search: location.search,
|
|
548
|
+
hash: location.hash
|
|
549
|
+
});
|
|
550
|
+
}, [location, notifyRouteChange]);
|
|
551
|
+
|
|
552
|
+
return null;
|
|
553
|
+
}
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
All three are no-ops outside iframe/WebView mode.
|
|
557
|
+
|
|
406
558
|
## SSR Support (Next.js)
|
|
407
559
|
|
|
408
560
|
Enable `ssr` on the client-side provider to sync the access token to a cookie. The cookie is written with `max-age` (matching the token lifetime) and `Secure` flag (on HTTPS origins) automatically.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { OAuth2Tokens, RestApiClient } from '@docyrus/api-client';
|
|
2
|
-
import { D as DocyrusAuthConfig, a as AuthMode, c as AuthStatus, g as DocyrusUser, f as DocyrusRole, P as PermissionConfig } from './types-
|
|
2
|
+
import { D as DocyrusAuthConfig, a as AuthMode, c as AuthStatus, g as DocyrusUser, R as RouteChangePayload, H as HostNavigationHandler, h as HostNotificationHandler, f as DocyrusRole, P as PermissionConfig } from './types-DjG8tL1X.js';
|
|
3
3
|
|
|
4
4
|
type AuthStateListener = (state: {
|
|
5
5
|
status: AuthStatus;
|
|
@@ -24,6 +24,8 @@ declare class AuthManager {
|
|
|
24
24
|
private reactNativeAuth;
|
|
25
25
|
private tokenRefreshTimer;
|
|
26
26
|
private listeners;
|
|
27
|
+
private navigationHandlers;
|
|
28
|
+
private notificationHandlers;
|
|
27
29
|
private config;
|
|
28
30
|
constructor(config?: DocyrusAuthConfig);
|
|
29
31
|
/** Whether the provider may refresh tokens on its own. Default: true. */
|
|
@@ -54,6 +56,31 @@ declare class AuthManager {
|
|
|
54
56
|
* Status remains 'loading' until the host sends the first signin message.
|
|
55
57
|
*/
|
|
56
58
|
private initializeIframeMode;
|
|
59
|
+
/**
|
|
60
|
+
* Iframe/WebView mode: start auto-syncing the embedded app's route
|
|
61
|
+
* to the host. Patches history methods and listens for popstate /
|
|
62
|
+
* hashchange. Idempotent. No-op outside iframe mode.
|
|
63
|
+
*/
|
|
64
|
+
enableHostRouteSync(): void;
|
|
65
|
+
/**
|
|
66
|
+
* Iframe/WebView mode: post a single `route-change` message to the
|
|
67
|
+
* host. With no argument, reads `window.location`. Pass a payload
|
|
68
|
+
* from a router subscription to report a known route. No-op outside
|
|
69
|
+
* iframe mode.
|
|
70
|
+
*/
|
|
71
|
+
notifyHostRouteChange(payload?: RouteChangePayload): void;
|
|
72
|
+
/**
|
|
73
|
+
* Subscribe to host `navigation` messages. Returns an unsubscribe function.
|
|
74
|
+
* Handlers registered before iframe mode is initialized are retained and
|
|
75
|
+
* will fire once messages arrive. No-op in non-iframe modes.
|
|
76
|
+
*/
|
|
77
|
+
onHostNavigation(handler: HostNavigationHandler): () => void;
|
|
78
|
+
/**
|
|
79
|
+
* Subscribe to host `notification` messages. Returns an unsubscribe function.
|
|
80
|
+
* Handlers registered before iframe mode is initialized are retained and
|
|
81
|
+
* will fire once messages arrive. No-op in non-iframe modes.
|
|
82
|
+
*/
|
|
83
|
+
onHostNotification(handler: HostNotificationHandler): () => void;
|
|
57
84
|
/**
|
|
58
85
|
* React Native mode initialization.
|
|
59
86
|
* No callback URL check needed — the in-app browser returns the URL directly.
|
package/dist/core-index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { A as AuthManager, e as AuthStateListener, D as DEFAULT_API_URL, c as DEFAULT_CALLBACK_PATH, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, h as hasPermission, b as hasRole } from './constants-
|
|
1
|
+
export { A as AuthManager, e as AuthStateListener, D as DEFAULT_API_URL, c as DEFAULT_CALLBACK_PATH, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, h as hasPermission, b as hasRole } from './constants-DuNDcs9I.js';
|
|
2
2
|
import { OAuth2Tokens } from '@docyrus/api-client';
|
|
3
|
-
import { D as DocyrusAuthConfig, a as AuthMode } from './types-
|
|
4
|
-
export { A as AclOperation, c as AuthStatus, d as DocyrusAclRule, e as DocyrusAuthContextValue, f as DocyrusRole, g as DocyrusUser,
|
|
3
|
+
import { D as DocyrusAuthConfig, R as RouteChangePayload, H as HostNavigationHandler, h as HostNotificationHandler, a as AuthMode } from './types-DjG8tL1X.js';
|
|
4
|
+
export { A as AclOperation, i as AppRouteChangeMessage, c as AuthStatus, d as DocyrusAclRule, e as DocyrusAuthContextValue, j as DocyrusNotification, f as DocyrusRole, g as DocyrusUser, k as HostNavigationMessage, l as HostNotificationMessage, m as HostSignInMessage, P as PermissionConfig, T as TokenRefreshRequestMessage } from './types-DjG8tL1X.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Standalone OAuth2 authorization code flow with PKCE via page redirect.
|
|
@@ -65,6 +65,9 @@ type IframeAuthCallback = (tokens: OAuth2Tokens) => void;
|
|
|
65
65
|
* - App → Host: { type: "signin-ready" } (on start)
|
|
66
66
|
* - Host → App: { type: "signin", accessToken, refreshToken }
|
|
67
67
|
* - App → Host: { type: "token-refresh-request" }
|
|
68
|
+
* - Host → App: { type: "navigation", url }
|
|
69
|
+
* - Host → App: { type: "notification", notification }
|
|
70
|
+
* - App → Host: { type: "route-change", path, search, hash, url }
|
|
68
71
|
*
|
|
69
72
|
* Every incoming message's event.origin is validated against
|
|
70
73
|
* the allowed host pattern before processing.
|
|
@@ -75,6 +78,13 @@ declare class IframeAuth {
|
|
|
75
78
|
private allowedPattern;
|
|
76
79
|
private isWebView;
|
|
77
80
|
private onTokensReceived;
|
|
81
|
+
private navigationHandlers;
|
|
82
|
+
private notificationHandlers;
|
|
83
|
+
private routeSyncEnabled;
|
|
84
|
+
private originalPushState;
|
|
85
|
+
private originalReplaceState;
|
|
86
|
+
private routeChangeListener;
|
|
87
|
+
private stopped;
|
|
78
88
|
private refreshPromise;
|
|
79
89
|
private messageHandler;
|
|
80
90
|
constructor(allowedOrigins?: string[], allowedPattern?: RegExp);
|
|
@@ -85,6 +95,29 @@ declare class IframeAuth {
|
|
|
85
95
|
start(onTokensReceived: IframeAuthCallback): void;
|
|
86
96
|
/** Stop listening for postMessage events. */
|
|
87
97
|
stop(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Patch history methods + listen for popstate/hashchange so every
|
|
100
|
+
* route change posts a `route-change` message to the host.
|
|
101
|
+
* Idempotent. Fires an immediate message with the current route.
|
|
102
|
+
*/
|
|
103
|
+
enableRouteSync(): void;
|
|
104
|
+
/**
|
|
105
|
+
* Post a single `route-change` message. With no argument, reads
|
|
106
|
+
* `window.location`. Pass a payload from a router subscription to
|
|
107
|
+
* report a known route instead.
|
|
108
|
+
*/
|
|
109
|
+
notifyRouteChange(payload?: RouteChangePayload): void;
|
|
110
|
+
private teardownRouteSync;
|
|
111
|
+
/**
|
|
112
|
+
* Subscribe to host navigation messages.
|
|
113
|
+
* Returns an unsubscribe function.
|
|
114
|
+
*/
|
|
115
|
+
onNavigation(handler: HostNavigationHandler): () => void;
|
|
116
|
+
/**
|
|
117
|
+
* Subscribe to host notification messages.
|
|
118
|
+
* Returns an unsubscribe function.
|
|
119
|
+
*/
|
|
120
|
+
onNotification(handler: HostNotificationHandler): () => void;
|
|
88
121
|
/**
|
|
89
122
|
* Request fresh tokens from the host.
|
|
90
123
|
* Posts a "token-refresh-request" to the host, then waits for
|
|
@@ -96,6 +129,9 @@ declare class IframeAuth {
|
|
|
96
129
|
requestTokenRefresh(): Promise<OAuth2Tokens>;
|
|
97
130
|
private isOriginAllowed;
|
|
98
131
|
private handleMessage;
|
|
132
|
+
private handleSignin;
|
|
133
|
+
private handleNavigation;
|
|
134
|
+
private handleNotification;
|
|
99
135
|
private rejectPendingRefresh;
|
|
100
136
|
}
|
|
101
137
|
|
|
@@ -108,4 +144,4 @@ declare class IframeAuth {
|
|
|
108
144
|
*/
|
|
109
145
|
declare function detectAuthMode(allowedOrigins?: string[], allowedPattern?: RegExp): AuthMode;
|
|
110
146
|
|
|
111
|
-
export { AuthMode, DocyrusAuthConfig, IframeAuth, StandaloneOAuth2Auth, detectAuthMode };
|
|
147
|
+
export { AuthMode, DocyrusAuthConfig, HostNavigationHandler, HostNotificationHandler, IframeAuth, RouteChangePayload, StandaloneOAuth2Auth, detectAuthMode };
|
package/dist/core-index.js
CHANGED
|
@@ -162,6 +162,13 @@ var IframeAuth = class {
|
|
|
162
162
|
allowedPattern;
|
|
163
163
|
isWebView;
|
|
164
164
|
onTokensReceived = null;
|
|
165
|
+
navigationHandlers = /* @__PURE__ */ new Set();
|
|
166
|
+
notificationHandlers = /* @__PURE__ */ new Set();
|
|
167
|
+
routeSyncEnabled = false;
|
|
168
|
+
originalPushState = null;
|
|
169
|
+
originalReplaceState = null;
|
|
170
|
+
routeChangeListener = null;
|
|
171
|
+
stopped = false;
|
|
165
172
|
refreshPromise = null;
|
|
166
173
|
messageHandler = null;
|
|
167
174
|
constructor(allowedOrigins, allowedPattern) {
|
|
@@ -181,11 +188,104 @@ var IframeAuth = class {
|
|
|
181
188
|
}
|
|
182
189
|
/** Stop listening for postMessage events. */
|
|
183
190
|
stop() {
|
|
191
|
+
this.stopped = true;
|
|
184
192
|
if (this.messageHandler) {
|
|
185
193
|
window.removeEventListener("message", this.messageHandler);
|
|
186
194
|
this.messageHandler = null;
|
|
187
195
|
}
|
|
188
196
|
this.rejectPendingRefresh("IframeAuth stopped");
|
|
197
|
+
this.navigationHandlers.clear();
|
|
198
|
+
this.notificationHandlers.clear();
|
|
199
|
+
this.teardownRouteSync();
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Patch history methods + listen for popstate/hashchange so every
|
|
203
|
+
* route change posts a `route-change` message to the host.
|
|
204
|
+
* Idempotent. Fires an immediate message with the current route.
|
|
205
|
+
*/
|
|
206
|
+
enableRouteSync() {
|
|
207
|
+
if (this.routeSyncEnabled || typeof window === "undefined") return;
|
|
208
|
+
this.routeSyncEnabled = true;
|
|
209
|
+
const { history } = window;
|
|
210
|
+
this.originalPushState = history.pushState.bind(history);
|
|
211
|
+
this.originalReplaceState = history.replaceState.bind(history);
|
|
212
|
+
history.pushState = (...args) => {
|
|
213
|
+
const result = this.originalPushState?.(...args);
|
|
214
|
+
this.notifyRouteChange();
|
|
215
|
+
return result;
|
|
216
|
+
};
|
|
217
|
+
history.replaceState = (...args) => {
|
|
218
|
+
const result = this.originalReplaceState?.(...args);
|
|
219
|
+
this.notifyRouteChange();
|
|
220
|
+
return result;
|
|
221
|
+
};
|
|
222
|
+
this.routeChangeListener = () => this.notifyRouteChange();
|
|
223
|
+
window.addEventListener("popstate", this.routeChangeListener);
|
|
224
|
+
window.addEventListener("hashchange", this.routeChangeListener);
|
|
225
|
+
this.notifyRouteChange();
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Post a single `route-change` message. With no argument, reads
|
|
229
|
+
* `window.location`. Pass a payload from a router subscription to
|
|
230
|
+
* report a known route instead.
|
|
231
|
+
*/
|
|
232
|
+
notifyRouteChange(payload) {
|
|
233
|
+
if (this.stopped) return;
|
|
234
|
+
let path;
|
|
235
|
+
let search;
|
|
236
|
+
let hash;
|
|
237
|
+
if (payload) {
|
|
238
|
+
path = payload.path ?? "";
|
|
239
|
+
search = payload.search ?? "";
|
|
240
|
+
hash = payload.hash ?? "";
|
|
241
|
+
} else if (typeof window !== "undefined") {
|
|
242
|
+
({ pathname: path, search, hash } = window.location);
|
|
243
|
+
} else {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
postToHost({
|
|
247
|
+
type: "route-change",
|
|
248
|
+
path,
|
|
249
|
+
search,
|
|
250
|
+
hash,
|
|
251
|
+
url: `${path}${search}${hash}`
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
teardownRouteSync() {
|
|
255
|
+
if (!this.routeSyncEnabled) return;
|
|
256
|
+
if (typeof window !== "undefined") {
|
|
257
|
+
const { history } = window;
|
|
258
|
+
if (this.originalPushState) history.pushState = this.originalPushState;
|
|
259
|
+
if (this.originalReplaceState) history.replaceState = this.originalReplaceState;
|
|
260
|
+
if (this.routeChangeListener) {
|
|
261
|
+
window.removeEventListener("popstate", this.routeChangeListener);
|
|
262
|
+
window.removeEventListener("hashchange", this.routeChangeListener);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
this.originalPushState = null;
|
|
266
|
+
this.originalReplaceState = null;
|
|
267
|
+
this.routeChangeListener = null;
|
|
268
|
+
this.routeSyncEnabled = false;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Subscribe to host navigation messages.
|
|
272
|
+
* Returns an unsubscribe function.
|
|
273
|
+
*/
|
|
274
|
+
onNavigation(handler) {
|
|
275
|
+
this.navigationHandlers.add(handler);
|
|
276
|
+
return () => {
|
|
277
|
+
this.navigationHandlers.delete(handler);
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Subscribe to host notification messages.
|
|
282
|
+
* Returns an unsubscribe function.
|
|
283
|
+
*/
|
|
284
|
+
onNotification(handler) {
|
|
285
|
+
this.notificationHandlers.add(handler);
|
|
286
|
+
return () => {
|
|
287
|
+
this.notificationHandlers.delete(handler);
|
|
288
|
+
};
|
|
189
289
|
}
|
|
190
290
|
/**
|
|
191
291
|
* Request fresh tokens from the host.
|
|
@@ -228,8 +328,20 @@ var IframeAuth = class {
|
|
|
228
328
|
handleMessage(ev) {
|
|
229
329
|
if (!this.isOriginAllowed(ev.origin)) return;
|
|
230
330
|
const data = ev.data;
|
|
231
|
-
if (!data || typeof data !== "object" || data.type !== "
|
|
232
|
-
|
|
331
|
+
if (!data || typeof data !== "object" || typeof data.type !== "string") return;
|
|
332
|
+
if (data.type === "signin") {
|
|
333
|
+
this.handleSignin(data);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (data.type === "navigation") {
|
|
337
|
+
this.handleNavigation(data);
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (data.type === "notification") {
|
|
341
|
+
this.handleNotification(data);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
handleSignin(signinData) {
|
|
233
345
|
if (!signinData.accessToken) return;
|
|
234
346
|
const tokens = {
|
|
235
347
|
accessToken: signinData.accessToken,
|
|
@@ -244,6 +356,29 @@ var IframeAuth = class {
|
|
|
244
356
|
this.refreshPromise = null;
|
|
245
357
|
}
|
|
246
358
|
}
|
|
359
|
+
handleNavigation(message) {
|
|
360
|
+
if (typeof message.url !== "string" || message.url.length === 0) return;
|
|
361
|
+
for (const handler of this.navigationHandlers) {
|
|
362
|
+
try {
|
|
363
|
+
handler(message);
|
|
364
|
+
} catch (err) {
|
|
365
|
+
console.warn("[docyrus-signin] host navigation handler threw:", err);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
handleNotification(message) {
|
|
370
|
+
const notification = message.notification;
|
|
371
|
+
if (!notification || typeof notification !== "object" || typeof notification.id !== "string") {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
for (const handler of this.notificationHandlers) {
|
|
375
|
+
try {
|
|
376
|
+
handler(notification);
|
|
377
|
+
} catch (err) {
|
|
378
|
+
console.warn("[docyrus-signin] host notification handler threw:", err);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
247
382
|
rejectPendingRefresh(reason) {
|
|
248
383
|
if (this.refreshPromise) {
|
|
249
384
|
clearTimeout(this.refreshPromise.timeoutId);
|
|
@@ -416,6 +551,8 @@ var AuthManager = class {
|
|
|
416
551
|
reactNativeAuth = null;
|
|
417
552
|
tokenRefreshTimer = null;
|
|
418
553
|
listeners = /* @__PURE__ */ new Set();
|
|
554
|
+
navigationHandlers = /* @__PURE__ */ new Set();
|
|
555
|
+
notificationHandlers = /* @__PURE__ */ new Set();
|
|
419
556
|
config;
|
|
420
557
|
constructor(config = {}) {
|
|
421
558
|
this.config = config;
|
|
@@ -554,6 +691,66 @@ var AuthManager = class {
|
|
|
554
691
|
this.iframeAuth.start((tokens) => {
|
|
555
692
|
this.setAuthenticated(tokens);
|
|
556
693
|
});
|
|
694
|
+
this.iframeAuth.onNavigation((message) => {
|
|
695
|
+
for (const handler of this.navigationHandlers) {
|
|
696
|
+
try {
|
|
697
|
+
handler(message);
|
|
698
|
+
} catch (err) {
|
|
699
|
+
console.warn("[docyrus-signin] host navigation handler threw:", err);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
});
|
|
703
|
+
this.iframeAuth.onNotification((notification) => {
|
|
704
|
+
for (const handler of this.notificationHandlers) {
|
|
705
|
+
try {
|
|
706
|
+
handler(notification);
|
|
707
|
+
} catch (err) {
|
|
708
|
+
console.warn("[docyrus-signin] host notification handler threw:", err);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
});
|
|
712
|
+
if (this.config.syncRouteToHost) {
|
|
713
|
+
this.iframeAuth.enableRouteSync();
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* Iframe/WebView mode: start auto-syncing the embedded app's route
|
|
718
|
+
* to the host. Patches history methods and listens for popstate /
|
|
719
|
+
* hashchange. Idempotent. No-op outside iframe mode.
|
|
720
|
+
*/
|
|
721
|
+
enableHostRouteSync() {
|
|
722
|
+
this.iframeAuth?.enableRouteSync();
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Iframe/WebView mode: post a single `route-change` message to the
|
|
726
|
+
* host. With no argument, reads `window.location`. Pass a payload
|
|
727
|
+
* from a router subscription to report a known route. No-op outside
|
|
728
|
+
* iframe mode.
|
|
729
|
+
*/
|
|
730
|
+
notifyHostRouteChange(payload) {
|
|
731
|
+
this.iframeAuth?.notifyRouteChange(payload);
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Subscribe to host `navigation` messages. Returns an unsubscribe function.
|
|
735
|
+
* Handlers registered before iframe mode is initialized are retained and
|
|
736
|
+
* will fire once messages arrive. No-op in non-iframe modes.
|
|
737
|
+
*/
|
|
738
|
+
onHostNavigation(handler) {
|
|
739
|
+
this.navigationHandlers.add(handler);
|
|
740
|
+
return () => {
|
|
741
|
+
this.navigationHandlers.delete(handler);
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Subscribe to host `notification` messages. Returns an unsubscribe function.
|
|
746
|
+
* Handlers registered before iframe mode is initialized are retained and
|
|
747
|
+
* will fire once messages arrive. No-op in non-iframe modes.
|
|
748
|
+
*/
|
|
749
|
+
onHostNotification(handler) {
|
|
750
|
+
this.notificationHandlers.add(handler);
|
|
751
|
+
return () => {
|
|
752
|
+
this.notificationHandlers.delete(handler);
|
|
753
|
+
};
|
|
557
754
|
}
|
|
558
755
|
/**
|
|
559
756
|
* React Native mode initialization.
|
|
@@ -821,6 +1018,8 @@ var AuthManager = class {
|
|
|
821
1018
|
this.clearTokenRefreshTimer();
|
|
822
1019
|
if (this.iframeAuth) this.iframeAuth.stop();
|
|
823
1020
|
this.listeners.clear();
|
|
1021
|
+
this.navigationHandlers.clear();
|
|
1022
|
+
this.notificationHandlers.clear();
|
|
824
1023
|
}
|
|
825
1024
|
};
|
|
826
1025
|
|