@iblai/web-utils 1.4.1 → 1.6.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
@@ -2527,6 +2527,7 @@ const COOKIE_KEYS = {
2527
2527
  USER_DATA: "ibl_user_data",
2528
2528
  TENANT: "ibl_tenant",
2529
2529
  LOGOUT_TIMESTAMP: "ibl_logout_timestamp",
2530
+ LOGIN_TIMESTAMP: "ibl_login_timestamp",
2530
2531
  };
2531
2532
  /**
2532
2533
  * Get the base domain for cookie sharing
@@ -2838,6 +2839,7 @@ function useAuthProvider({ middleware = new Map(), onAuthSuccess, onAuthFailure,
2838
2839
  const [initialSyncComplete, setInitialSyncComplete] = React.useState(false);
2839
2840
  const cookieCheckIntervalRef = React.useRef(null);
2840
2841
  const lastLogoutTimestampRef = React.useRef(null);
2842
+ const lastLoginTimestampRef = React.useRef(null);
2841
2843
  // Guard to prevent poll from firing after a redirect has been initiated.
2842
2844
  // Once set, no further redirects or cookie syncs will happen.
2843
2845
  const isRedirectingRef = React.useRef(false);
@@ -2893,8 +2895,9 @@ function useAuthProvider({ middleware = new Map(), onAuthSuccess, onAuthFailure,
2893
2895
  async function initialSync() {
2894
2896
  console.log("[AuthProvider] initialSync starting");
2895
2897
  try {
2896
- // Initialize last known logout timestamp
2898
+ // Initialize last known logout and login timestamps
2897
2899
  lastLogoutTimestampRef.current = CookieUtils.get(COOKIE_KEYS.LOGOUT_TIMESTAMP);
2900
+ lastLoginTimestampRef.current = CookieUtils.get(COOKIE_KEYS.LOGIN_TIMESTAMP);
2898
2901
  const { needsRefresh, userDataOutOfSync } = await syncCookiesToLocalStorage(storageService);
2899
2902
  console.log("[AuthProvider] initialSync result", {
2900
2903
  needsRefresh,
@@ -2947,6 +2950,8 @@ function useAuthProvider({ middleware = new Map(), onAuthSuccess, onAuthFailure,
2947
2950
  needsRefresh,
2948
2951
  userDataOutOfSync,
2949
2952
  pathname,
2953
+ currentLogoutTimestamp,
2954
+ lastLogoutTimestampRef: lastLogoutTimestampRef.current,
2950
2955
  });
2951
2956
  if (needsRefresh) {
2952
2957
  console.log("[auth-redirect] Cookie sync detected changes from another SPA, refreshing page");
@@ -3223,7 +3228,7 @@ const useTenantContext = () => React.useContext(TenantContext);
3223
3228
  * 4. Handles tenant-specific domain redirects
3224
3229
  * 5. Maintains tenant access state
3225
3230
  */
3226
- function TenantProvider({ children, fallback, onAuthSuccess, onAuthFailure, currentTenant, requestedTenant, saveCurrentTenant, saveUserTenants, handleTenantSwitch, saveVisitingTenant, removeVisitingTenant, saveUserTokens, saveTenant, onAutoJoinUserToTenant, redirectToAuthSpa, username, isIframed = false, setUseMentorProvider, skip = false, onLoadPlatformPermissions, skipCustomDomainCheck = false, }) {
3231
+ function TenantProvider({ children, fallback, onAuthSuccess, onAuthFailure, currentTenant, requestedTenant, saveCurrentTenant, saveUserTenants, handleTenantSwitch, saveVisitingTenant, removeVisitingTenant, saveUserTokens, saveTenant, onAutoJoinUserToTenant, redirectToAuthSpa, username, isIframed = false, setUseMentorProvider, skip = false, onLoadPlatformPermissions, skipCustomDomainCheck = false, onTenantMismatch = () => { }, }) {
3227
3232
  // If skip is true, just return children without any provider logic
3228
3233
  if (skip) {
3229
3234
  return jsxRuntime.jsx(jsxRuntime.Fragment, { children: children });
@@ -3654,6 +3659,30 @@ function TenantProvider({ children, fallback, onAuthSuccess, onAuthFailure, curr
3654
3659
  isLoadingCustomDomain,
3655
3660
  isCustomDomainError,
3656
3661
  ]);
3662
+ // Poll every 2s: if requestedTenant exists, differs from currentTenant,
3663
+ // and user is not on a public route, invoke the onTenantMismatch callback.
3664
+ React.useEffect(() => {
3665
+ if (!onTenantMismatch)
3666
+ return;
3667
+ const interval = setInterval(() => {
3668
+ if (requestedTenant &&
3669
+ currentTenant &&
3670
+ requestedTenant !== currentTenant &&
3671
+ !userIsAccessingPublicRoute) {
3672
+ console.log("[TenantProvider] Tenant mismatch detected", {
3673
+ requestedTenant,
3674
+ currentTenant,
3675
+ });
3676
+ onTenantMismatch();
3677
+ }
3678
+ }, 2000);
3679
+ return () => clearInterval(interval);
3680
+ }, [
3681
+ requestedTenant,
3682
+ currentTenant,
3683
+ userIsAccessingPublicRoute,
3684
+ onTenantMismatch,
3685
+ ]);
3657
3686
  // Show fallback component during tenant determination
3658
3687
  if (isLoading) {
3659
3688
  return fallback;