azirid-react 0.9.9 → 0.10.1

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.cjs CHANGED
@@ -165,6 +165,31 @@ function createAccessClient(config, appContext) {
165
165
  let refreshToken = ssGet(storageKeyRt);
166
166
  let csrfToken = ssGet(storageKeyCsrf);
167
167
  let refreshPromise = null;
168
+ let channel = null;
169
+ try {
170
+ if (typeof BroadcastChannel !== "undefined") {
171
+ channel = new BroadcastChannel("azirid-auth");
172
+ channel.onmessage = (event) => {
173
+ if (event.data?.type === "token-refreshed") {
174
+ accessToken = event.data.accessToken;
175
+ if (event.data.refreshToken) setRefreshToken(event.data.refreshToken);
176
+ if (event.data.csrfToken) setCsrfToken(event.data.csrfToken);
177
+ }
178
+ };
179
+ }
180
+ } catch {
181
+ }
182
+ function broadcastTokens() {
183
+ try {
184
+ channel?.postMessage({
185
+ type: "token-refreshed",
186
+ accessToken,
187
+ refreshToken,
188
+ csrfToken
189
+ });
190
+ } catch {
191
+ }
192
+ }
168
193
  function setAccessToken(token) {
169
194
  accessToken = token;
170
195
  }
@@ -226,6 +251,7 @@ function createAccessClient(config, appContext) {
226
251
  const xc = json.xc ?? json.csrfToken;
227
252
  if (rt) setRefreshToken(rt);
228
253
  if (xc) setCsrfToken(xc);
254
+ broadcastTokens();
229
255
  }
230
256
  function refreshTokens(opts) {
231
257
  if (opts?.tenantId) {
@@ -578,6 +604,7 @@ function useAuthMutations(deps) {
578
604
  updateAccessToken(normalizeToken(data));
579
605
  saveSessionTokens(data);
580
606
  setError(null);
607
+ props.onAuthStateChange?.();
581
608
  props.onLoginSuccess?.(data);
582
609
  },
583
610
  onError: (err) => {
@@ -595,6 +622,7 @@ function useAuthMutations(deps) {
595
622
  updateAccessToken(normalizeToken(data));
596
623
  saveSessionTokens(data);
597
624
  setError(null);
625
+ props.onAuthStateChange?.();
598
626
  props.onSignupSuccess?.(data);
599
627
  },
600
628
  onError: (err) => {
@@ -608,6 +636,7 @@ function useAuthMutations(deps) {
608
636
  clearSession();
609
637
  setError(null);
610
638
  queryClient.clear();
639
+ props.onAuthStateChange?.();
611
640
  props.onLogoutSuccess?.();
612
641
  }
613
642
  });
@@ -775,6 +804,27 @@ function AziridProviderInner({
775
804
  }, [client, props.autoBootstrap, updateAccessToken, saveSessionTokens]);
776
805
  const silentRefresh = react.useCallback(async () => {
777
806
  if (!client.getAccessToken()) return;
807
+ if (typeof navigator !== "undefined" && "locks" in navigator) {
808
+ let acquired = false;
809
+ try {
810
+ await navigator.locks.request(
811
+ "azirid-token-refresh",
812
+ { ifAvailable: true },
813
+ async (lock) => {
814
+ if (!lock) return;
815
+ acquired = true;
816
+ await client.refreshSession();
817
+ updateAccessToken(client.getAccessToken());
818
+ }
819
+ );
820
+ } catch (err) {
821
+ if (acquired && isAuthError(err)) {
822
+ clearSession();
823
+ props.onSessionExpired?.();
824
+ }
825
+ }
826
+ return;
827
+ }
778
828
  try {
779
829
  await client.refreshSession();
780
830
  updateAccessToken(client.getAccessToken());
@@ -785,6 +835,27 @@ function AziridProviderInner({
785
835
  }
786
836
  }
787
837
  }, [client, updateAccessToken, clearSession, props]);
838
+ react.useEffect(() => {
839
+ let channel = null;
840
+ try {
841
+ if (typeof BroadcastChannel !== "undefined") {
842
+ channel = new BroadcastChannel("azirid-auth");
843
+ channel.onmessage = (event) => {
844
+ if (event.data?.type === "token-refreshed" && event.data.accessToken) {
845
+ client.setAccessToken(event.data.accessToken);
846
+ updateAccessToken(event.data.accessToken);
847
+ }
848
+ };
849
+ }
850
+ } catch {
851
+ }
852
+ return () => {
853
+ try {
854
+ channel?.close();
855
+ } catch {
856
+ }
857
+ };
858
+ }, [client, updateAccessToken]);
788
859
  react.useEffect(() => {
789
860
  const intervalMs = props.refreshInterval ?? 5e4;
790
861
  if (intervalMs <= 0) return;
@@ -4275,7 +4346,7 @@ function usePasswordToggle() {
4275
4346
  }
4276
4347
 
4277
4348
  // src/index.ts
4278
- var SDK_VERSION = "0.9.9";
4349
+ var SDK_VERSION = "0.10.1";
4279
4350
 
4280
4351
  exports.AuthForm = AuthForm;
4281
4352
  exports.AziridProvider = AziridProvider;