Skip to content

[Bug] Multi-tenant Logout State Not Properly Cleared #405

@Clawiee

Description

@Clawiee

🐛 Bug: Multi-tenant Logout State Not Properly Cleared

Description

When a user logs into Company A, switches to Company B, logs out from Company B, and then uses the browser's back button, the user from Company A still appears logged in.

Steps to Reproduce

  1. Log in to Company A
  2. Switch to Company B
  3. Log out from Company B
  4. Click browser back button
  5. Observe: Company A's account still shows as logged in

Root Cause Analysis

Code Location:

  • frontend/src/stores/index.ts - logout function
  • frontend/src/App.tsx - App initialization logic

Issue 1: Incomplete Logout Cleanup
The current logout function only clears token and user, but doesn't clear other authentication-related data:

logout: () => {
    localStorage.removeItem('token');
    set({ user: null, token: null });
},

Issue 2: Browser Back Cache State
When using browser back button, the page may restore from cache without full re-initialization. The App.tsx initialization logic may not properly re-validate the user state:

if (effectiveToken && !user) {
    authApi.me()
        .then((u) => setAuth(u, effectiveToken!))
        .catch(() => useAuthStore.getState().logout())
        .finally(() => setLoading(false));
}

Suggested Fix

Option 1: Enhanced Logout Function

logout: () => {
    localStorage.removeItem('token');
    localStorage.removeItem('user');
    localStorage.removeItem('current_tenant_id');
    sessionStorage.clear();
    set({ user: null, token: null });
},

Option 2: Add Validation in App Initialization
Verify that the returned user context matches the expected tenant before restoring session.

Labels

  • bug
  • multi-tenant
  • security

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingsecurity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions