@docyrus/signin 0.9.0 → 0.10.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
@@ -165,6 +165,8 @@ var IframeAuth = class {
165
165
  allowedPattern;
166
166
  isWebView;
167
167
  onTokensReceived = null;
168
+ navigationHandlers = /* @__PURE__ */ new Set();
169
+ notificationHandlers = /* @__PURE__ */ new Set();
168
170
  refreshPromise = null;
169
171
  messageHandler = null;
170
172
  constructor(allowedOrigins, allowedPattern) {
@@ -189,6 +191,28 @@ var IframeAuth = class {
189
191
  this.messageHandler = null;
190
192
  }
191
193
  this.rejectPendingRefresh("IframeAuth stopped");
194
+ this.navigationHandlers.clear();
195
+ this.notificationHandlers.clear();
196
+ }
197
+ /**
198
+ * Subscribe to host navigation messages.
199
+ * Returns an unsubscribe function.
200
+ */
201
+ onNavigation(handler) {
202
+ this.navigationHandlers.add(handler);
203
+ return () => {
204
+ this.navigationHandlers.delete(handler);
205
+ };
206
+ }
207
+ /**
208
+ * Subscribe to host notification messages.
209
+ * Returns an unsubscribe function.
210
+ */
211
+ onNotification(handler) {
212
+ this.notificationHandlers.add(handler);
213
+ return () => {
214
+ this.notificationHandlers.delete(handler);
215
+ };
192
216
  }
193
217
  /**
194
218
  * Request fresh tokens from the host.
@@ -231,8 +255,20 @@ var IframeAuth = class {
231
255
  handleMessage(ev) {
232
256
  if (!this.isOriginAllowed(ev.origin)) return;
233
257
  const data = ev.data;
234
- if (!data || typeof data !== "object" || data.type !== "signin") return;
235
- const signinData = data;
258
+ if (!data || typeof data !== "object" || typeof data.type !== "string") return;
259
+ if (data.type === "signin") {
260
+ this.handleSignin(data);
261
+ return;
262
+ }
263
+ if (data.type === "navigation") {
264
+ this.handleNavigation(data);
265
+ return;
266
+ }
267
+ if (data.type === "notification") {
268
+ this.handleNotification(data);
269
+ }
270
+ }
271
+ handleSignin(signinData) {
236
272
  if (!signinData.accessToken) return;
237
273
  const tokens = {
238
274
  accessToken: signinData.accessToken,
@@ -247,6 +283,29 @@ var IframeAuth = class {
247
283
  this.refreshPromise = null;
248
284
  }
249
285
  }
286
+ handleNavigation(message) {
287
+ if (typeof message.url !== "string" || message.url.length === 0) return;
288
+ for (const handler of this.navigationHandlers) {
289
+ try {
290
+ handler(message);
291
+ } catch (err) {
292
+ console.warn("[docyrus-signin] host navigation handler threw:", err);
293
+ }
294
+ }
295
+ }
296
+ handleNotification(message) {
297
+ const notification = message.notification;
298
+ if (!notification || typeof notification !== "object" || typeof notification.id !== "string") {
299
+ return;
300
+ }
301
+ for (const handler of this.notificationHandlers) {
302
+ try {
303
+ handler(notification);
304
+ } catch (err) {
305
+ console.warn("[docyrus-signin] host notification handler threw:", err);
306
+ }
307
+ }
308
+ }
250
309
  rejectPendingRefresh(reason) {
251
310
  if (this.refreshPromise) {
252
311
  clearTimeout(this.refreshPromise.timeoutId);
@@ -425,6 +484,8 @@ var AuthManager = class {
425
484
  reactNativeAuth = null;
426
485
  tokenRefreshTimer = null;
427
486
  listeners = /* @__PURE__ */ new Set();
487
+ navigationHandlers = /* @__PURE__ */ new Set();
488
+ notificationHandlers = /* @__PURE__ */ new Set();
428
489
  config;
429
490
  constructor(config = {}) {
430
491
  this.config = config;
@@ -563,6 +624,46 @@ var AuthManager = class {
563
624
  this.iframeAuth.start((tokens) => {
564
625
  this.setAuthenticated(tokens);
565
626
  });
627
+ this.iframeAuth.onNavigation((message) => {
628
+ for (const handler of this.navigationHandlers) {
629
+ try {
630
+ handler(message);
631
+ } catch (err) {
632
+ console.warn("[docyrus-signin] host navigation handler threw:", err);
633
+ }
634
+ }
635
+ });
636
+ this.iframeAuth.onNotification((notification) => {
637
+ for (const handler of this.notificationHandlers) {
638
+ try {
639
+ handler(notification);
640
+ } catch (err) {
641
+ console.warn("[docyrus-signin] host notification handler threw:", err);
642
+ }
643
+ }
644
+ });
645
+ }
646
+ /**
647
+ * Subscribe to host `navigation` messages. Returns an unsubscribe function.
648
+ * Handlers registered before iframe mode is initialized are retained and
649
+ * will fire once messages arrive. No-op in non-iframe modes.
650
+ */
651
+ onHostNavigation(handler) {
652
+ this.navigationHandlers.add(handler);
653
+ return () => {
654
+ this.navigationHandlers.delete(handler);
655
+ };
656
+ }
657
+ /**
658
+ * Subscribe to host `notification` messages. Returns an unsubscribe function.
659
+ * Handlers registered before iframe mode is initialized are retained and
660
+ * will fire once messages arrive. No-op in non-iframe modes.
661
+ */
662
+ onHostNotification(handler) {
663
+ this.notificationHandlers.add(handler);
664
+ return () => {
665
+ this.notificationHandlers.delete(handler);
666
+ };
566
667
  }
567
668
  /**
568
669
  * React Native mode initialization.
@@ -733,9 +834,10 @@ var AuthManager = class {
733
834
  */
734
835
  async getValidToken() {
735
836
  if (!this.tokens) return null;
736
- if (this.autoRefreshEnabled && this.tokens.expiresAt) {
837
+ const expiresAt = this.resolveExpiry(this.tokens);
838
+ if (this.autoRefreshEnabled && expiresAt) {
737
839
  const nowSec = Math.floor(Date.now() / 1e3);
738
- if (nowSec >= this.tokens.expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS) {
840
+ if (nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS) {
739
841
  try {
740
842
  const freshToken = await this.handleTokenRefresh();
741
843
  return freshToken;
@@ -779,8 +881,9 @@ var AuthManager = class {
779
881
  scheduleTokenRefresh(tokens) {
780
882
  this.clearTokenRefreshTimer();
781
883
  if (!this.autoRefreshEnabled) return;
782
- if (!tokens.expiresAt) return;
783
- const expiresAtMs = tokens.expiresAt * 1e3;
884
+ const expiresAt = this.resolveExpiry(tokens);
885
+ if (!expiresAt) return;
886
+ const expiresAtMs = expiresAt * 1e3;
784
887
  const bufferMs = TOKEN_EXPIRY_BUFFER_SECONDS * 1e3;
785
888
  const refreshInMs = expiresAtMs - bufferMs - Date.now();
786
889
  if (refreshInMs <= 0) {
@@ -805,11 +908,31 @@ var AuthManager = class {
805
908
  this.tokenRefreshTimer = null;
806
909
  }
807
910
  }
911
+ /**
912
+ * Resolve token expiry from stored expiresAt, falling back to the JWT exp
913
+ * claim when expiresAt is absent (e.g. server omitted expires_in).
914
+ */
915
+ resolveExpiry(tokens) {
916
+ if (tokens.expiresAt) return tokens.expiresAt;
917
+ const parts = tokens.accessToken?.split(".");
918
+ if (parts?.length >= 2) {
919
+ try {
920
+ const payload = JSON.parse(
921
+ atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
922
+ );
923
+ if (typeof payload["exp"] === "number") return payload["exp"];
924
+ } catch {
925
+ }
926
+ }
927
+ return null;
928
+ }
808
929
  /** Cleanup: remove listeners, timers, stop iframe auth. */
809
930
  destroy() {
810
931
  this.clearTokenRefreshTimer();
811
932
  if (this.iframeAuth) this.iframeAuth.stop();
812
933
  this.listeners.clear();
934
+ this.navigationHandlers.clear();
935
+ this.notificationHandlers.clear();
813
936
  }
814
937
  };
815
938
 
@@ -975,6 +1098,18 @@ function DocyrusAuthProvider({
975
1098
  await managerRef.current?.refreshUser();
976
1099
  };
977
1100
  }, []);
1101
+ const onHostNavigation = useMemo(() => {
1102
+ return (handler) => {
1103
+ return managerRef.current?.onHostNavigation(handler) ?? (() => {
1104
+ });
1105
+ };
1106
+ }, []);
1107
+ const onHostNotification = useMemo(() => {
1108
+ return (handler) => {
1109
+ return managerRef.current?.onHostNotification(handler) ?? (() => {
1110
+ });
1111
+ };
1112
+ }, []);
978
1113
  const value = useMemo(() => ({
979
1114
  status,
980
1115
  mode,
@@ -988,6 +1123,8 @@ function DocyrusAuthProvider({
988
1123
  hasRole: hasRole2,
989
1124
  hasPermission: hasPermission2,
990
1125
  refreshUser,
1126
+ onHostNavigation,
1127
+ onHostNotification,
991
1128
  error
992
1129
  }), [
993
1130
  status,
@@ -1002,6 +1139,8 @@ function DocyrusAuthProvider({
1002
1139
  hasRole2,
1003
1140
  hasPermission2,
1004
1141
  refreshUser,
1142
+ onHostNavigation,
1143
+ onHostNotification,
1005
1144
  error
1006
1145
  ]);
1007
1146
  return /* @__PURE__ */ jsx(DocyrusAuthContext, { value, children });
@@ -1021,6 +1160,26 @@ function useDocyrusClient() {
1021
1160
  const { client } = useDocyrusAuth();
1022
1161
  return client;
1023
1162
  }
1163
+ function useDocyrusHostNavigation(handler) {
1164
+ const { onHostNavigation } = useDocyrusAuth();
1165
+ const handlerRef = useRef(handler);
1166
+ handlerRef.current = handler;
1167
+ useEffect(() => {
1168
+ return onHostNavigation((message) => {
1169
+ handlerRef.current(message);
1170
+ });
1171
+ }, [onHostNavigation]);
1172
+ }
1173
+ function useDocyrusHostNotification(handler) {
1174
+ const { onHostNotification } = useDocyrusAuth();
1175
+ const handlerRef = useRef(handler);
1176
+ handlerRef.current = handler;
1177
+ useEffect(() => {
1178
+ return onHostNotification((notification) => {
1179
+ handlerRef.current(notification);
1180
+ });
1181
+ }, [onHostNotification]);
1182
+ }
1024
1183
  var ICON_SIZE = 20;
1025
1184
  function DocyrusLogo({ size = ICON_SIZE }) {
1026
1185
  return /* @__PURE__ */ jsx(
@@ -1105,6 +1264,6 @@ function SignInButton({
1105
1264
  );
1106
1265
  }
1107
1266
 
1108
- export { AuthManager, DEFAULT_API_URL, DEFAULT_CALLBACK_PATH, DEFAULT_OAUTH_SCOPES, DEFAULT_SSR_COOKIE_KEY, DocyrusAuthContext, DocyrusAuthProvider, IframeAuth, SignInButton, StandaloneOAuth2Auth, clearTokenCookie, detectAuthMode, getAllRoles, getTokenFromCookie, hasPermission, hasRole, syncTokenToCookie, useDocyrusAuth, useDocyrusClient };
1267
+ export { AuthManager, DEFAULT_API_URL, DEFAULT_CALLBACK_PATH, DEFAULT_OAUTH_SCOPES, DEFAULT_SSR_COOKIE_KEY, DocyrusAuthContext, DocyrusAuthProvider, IframeAuth, SignInButton, StandaloneOAuth2Auth, clearTokenCookie, detectAuthMode, getAllRoles, getTokenFromCookie, hasPermission, hasRole, syncTokenToCookie, useDocyrusAuth, useDocyrusClient, useDocyrusHostNavigation, useDocyrusHostNotification };
1109
1268
  //# sourceMappingURL=index.js.map
1110
1269
  //# sourceMappingURL=index.js.map