Fix enterprise demo tenant loading race condition

Prevent loadUserTenants from being called too early for demo sessions by excluding demo mode from the authenticated user auto-load logic.

## Problem

In enterprise demo sessions, loadUserTenants() was being called BEFORE child tenants were created, resulting in only the parent tenant (1 tenant) being returned instead of all 6 tenants (parent + 5 children).

Timeline of the race condition:
- 15:37:20: Session created, parent tenant cloning started
- 15:37:21: loadUserTenants() called by first useEffect → returned 1 tenant 
- 15:37:22: Child tenants created (5 tenants)
- 15:38:03: Session status changed to 'ready'

The first useEffect (for authenticated users) triggered immediately when isAuthenticated became true, calling loadUserTenants() at 15:37:21. This happened BEFORE the session polling logic in the second useEffect could wait for status='ready' and BEFORE child tenants were created.

## Solution

Added `!isDemoMode` condition to the first useEffect that auto-loads tenants for authenticated users. This ensures demo sessions use only the special initialization logic with session status polling (second useEffect), which waits for the session to be fully ready before loading tenants.

## Files Changed

- frontend/src/stores/useTenantInitializer.ts:61
  - Added `&& !isDemoMode` to prevent early tenant loading for demo users
  - Added dependency `isDemoMode` to useEffect dependency array
  - Updated comment to clarify demo users have separate initialization

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Urtzi Alfaro
2025-12-17 16:43:45 +01:00
parent 2619a34041
commit 6f5e8b11f6

View File

@@ -56,12 +56,12 @@ export const useTenantInitializer = () => {
const currentTenant = useCurrentTenant();
const { loadUserTenants, setCurrentTenant } = useTenantActions();
// Load tenants for authenticated users
// Load tenants for authenticated users (but not demo users - they have special initialization below)
useEffect(() => {
if (isAuthenticated && !availableTenants) {
if (isAuthenticated && !availableTenants && !isDemoMode) {
loadUserTenants();
}
}, [isAuthenticated, availableTenants, loadUserTenants]);
}, [isAuthenticated, availableTenants, loadUserTenants, isDemoMode]);
// Set up mock tenant for demo mode with appropriate subscription tier
useEffect(() => {