Skip to content

fix: 'No tenant selected' error during onboarding - #372

Open
dimavedenyapin wants to merge 1 commit into
mainfrom
fix/tenant-auth-race-condition
Open

fix: 'No tenant selected' error during onboarding#372
dimavedenyapin wants to merge 1 commit into
mainfrom
fix/tenant-auth-race-condition

Conversation

@dimavedenyapin

@dimavedenyapin dimavedenyapin commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • During onboarding, after login + tenant selection, the dashboard shows: "No tenant selected — please select an organization"
  • Root cause: selectTenant() stores JWT + tenant ID, then calls fetchAndStoreAiGatewayVirtualKey(). If that call triggers a 401 retry that fails, apiRequest()'s catch block calls clearStoredData()wiping the JWT and tenant ID that were just stored. selectTenant() catches this as a non-fatal warning, but the DB is already empty.

What changed

1. EnterpriseAuthError class with AuthErrorCode enum (enterprise-auth.ts)

  • Typed error class replaces raw throw new Error(string) for all auth-related failures
  • Error codes: SESSION_EXPIRED, NO_REFRESH_TOKEN, NO_TENANT, NOT_AUTHENTICATED, PERMISSION_DENIED, API_ERROR

2. Prevent double clearStoredData() (enterprise-auth.ts)

  • When executeRefresh() throws a typed EnterpriseAuthError (which already ran clearStoredData() if needed), apiRequest()'s 401-handler now propagates it directly instead of wrapping and clearing again
  • This is the actual fix — the double-clear was nuking tenant state during selectTenant's sub-call

3. IPC error code propagation (ipc-handlers.ts)

  • Electron IPC only serializes Error.message — custom fields are lost
  • enterprise:apiRequest handler now wraps EnterpriseAuthError as "[CODE] message" so the renderer gets structured codes

4. Dashboard uses error codes (dashboard-store.ts)

  • isAuthError() now checks structured [CODE] prefixes first
  • Legacy string patterns kept as fallback for pre-migration errors
  • No more fragile string matching for new error paths

Test plan

  • Login → select tenant → dashboard loads without "No tenant selected" error
  • If AI gateway key fetch fails → session survives, dashboard still works
  • Genuine session expiry → dashboard triggers re-login flow
  • Error codes visible in IPC errors: [SESSION_EXPIRED] Session expired...

🤖 Generated with Claude Code

…selectTenant

Three changes that fix the "No tenant selected" error during onboarding:

1. EnterpriseAuthError class with AuthErrorCode enum — typed errors
   propagated through IPC as "[CODE] message" so the renderer can do
   structured error handling instead of fragile string matching.

2. apiRequest 401-handler now checks if the error is already a typed
   EnterpriseAuthError (from executeRefresh) and propagates it directly
   instead of calling clearStoredData() a second time. This prevents the
   double-clear that nuked the JWT + tenant ID during selectTenant's
   fetchAndStoreAiGatewayVirtualKey sub-call.

3. Dashboard isAuthError() now checks structured codes first, with
   legacy string fallback for pre-migration errors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@dimavedenyapin
dimavedenyapin force-pushed the fix/tenant-auth-race-condition branch from ed0f441 to 6af9348 Compare June 10, 2026 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant