@pubflow/react 0.4.2 → 0.4.4

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.esm.js CHANGED
@@ -206,7 +206,8 @@ class BrowserStorageAdapter {
206
206
  */
207
207
  const PubflowContext = createContext({
208
208
  instances: {},
209
- defaultInstance: 'default'
209
+ defaultInstance: 'default',
210
+ isReady: false
210
211
  });
211
212
  /**
212
213
  * Pubflow Provider Component
@@ -214,7 +215,8 @@ const PubflowContext = createContext({
214
215
  function PubflowProvider({ children, config, instances, defaultInstance = 'default', enableDebugTools = false, showSessionAlerts = false, persistentCache = { enabled: false }, theme = {}, loginRedirectPath = '/login', publicPaths = [] }) {
215
216
  const [contextValue, setContextValue] = useState({
216
217
  instances: {},
217
- defaultInstance
218
+ defaultInstance,
219
+ isReady: false
218
220
  });
219
221
  // Session handlers
220
222
  const handleSessionExpired = useCallback(() => {
@@ -459,9 +461,14 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
459
461
  if (isMounted && Object.keys(instancesMap).length > 0) {
460
462
  setContextValue({
461
463
  instances: instancesMap,
462
- defaultInstance
464
+ defaultInstance,
465
+ isReady: true
463
466
  });
464
467
  }
468
+ else if (isMounted) {
469
+ // Even if no instances configured correctly, mark ready so useAuth can react
470
+ setContextValue(prev => ({ ...prev, isReady: true }));
471
+ }
465
472
  }
466
473
  catch (error) {
467
474
  console.error('Error initializing Pubflow:', error);
@@ -512,6 +519,19 @@ function useAuth(instanceId) {
512
519
  }
513
520
  const instance = instanceId || context.defaultInstance;
514
521
  const pubflowInstance = context.instances[instance];
522
+ // If the provider hasn't finished its initial useEffect setup,
523
+ // instances won't exist yet. Return a loading state instead of crashing.
524
+ if (!context.isReady) {
525
+ return {
526
+ user: null,
527
+ isAuthenticated: false,
528
+ isLoading: true, // Crucial: tell the UI we are still loading the provider
529
+ login: async () => { throw new Error('Pubflow not initialized yet'); },
530
+ logout: async () => { throw new Error('Pubflow not initialized yet'); },
531
+ validateSession: async () => ({ isValid: false }),
532
+ };
533
+ }
534
+ // After provider is ready, if the instance is STILL missing, it's a real config error.
515
535
  if (!pubflowInstance) {
516
536
  throw new Error(`Pubflow instance '${instance}' not found`);
517
537
  }