Clear auth store when exiting demo session to prevent unauthorized page redirect.
## Problem
When users clicked "Salir" (Exit) from the demo session, they were redirected to the unauthorized page (`/unauthorized`) instead of the demo landing page (`/demo`).
## Root Cause
The `handleExpiration()` function in DemoBanner.tsx was clearing localStorage and navigating to `/demo`, but was NOT clearing the auth store. This created an inconsistent state:
- `isDemoMode = false` (localStorage cleared)
- `demoSessionId = null` (localStorage cleared)
- `isAuthenticated = true` (auth store NOT cleared - still has demo user)
The `useHasAccess()` hook checks:
```typescript
return isAuthenticated || (isDemoMode && !!demoSessionId);
```
After clearing localStorage but not auth:
- `isAuthenticated = true` but the demo session is invalid
- `isDemoMode = false` and `demoSessionId = null`
- Result: `useHasAccess()` returns `false`
When navigating to `/demo`, the ProtectedRoute checked access and found it was `false`, redirecting to `/unauthorized`.
## Solution
Call `logout()` on the auth store before navigating to clear the demo user session completely. This ensures:
- Auth store is cleared (`isAuthenticated = false`)
- User is properly logged out from demo session
- Navigation to `/demo` succeeds without authentication check
## Additional Improvements
- Also clear `virtual_tenant_id` and `subscription_tier` from localStorage
- Updated comment to clarify navigation intent
## Files Changed
- frontend/src/components/layout/DemoBanner/DemoBanner.tsx:73-74
- Added auth store logout before navigation
- Added clearing of virtual_tenant_id and subscription_tier
- Updated comment for clarity
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
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>
Implemented polling mechanism to wait for demo session to be fully ready
before attempting to load tenant list.
## The Problem:
Frontend was calling GET /tenants/user/demo-user/tenants immediately after
session creation, but tenant cloning is asynchronous and takes 5-10 seconds.
This caused the API to return empty array [] even though it returned 200 OK.
## The Solution:
Added pollSessionStatus() function that:
1. Polls /api/v1/demo-sessions/{session_id}/status every 1 second
2. Waits for status to become 'ready' (max 30 seconds)
3. Only loads tenants after session is confirmed ready
4. Logs detailed status updates for debugging
## Flow (Before):
User clicks demo → Session created → API called immediately → Empty []
## Flow (After):
User clicks demo → Session created → Poll status (1s interval) →
Status: initializing → cloning_data → ready → Load tenants → Success!
## Benefits:
✅ No more empty tenant lists due to timing
✅ Clear console logs showing progress
✅ Handles session failures gracefully
✅ Timeout protection (30 seconds max)
✅ Works for both enterprise (6 tenants) and professional (1 tenant)
## Console Output Example:
⏳ Session status poll 1/30: initializing
⏳ Session status poll 2/30: cloning_data
⏳ Session status poll 8/30: ready
✅ Demo session is ready!
🔄 Loading available tenants for enterprise demo...
📋 Loaded available tenants: 6
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>