Home Projects Portfolio Dashboard Export PDF Log in

Streamlining Multi-Tenancy: Implementing Company Switching in Ryuu-no-Mi

In the development of Ryuu-no-Mi/Gestion-de-flotas, our fleet management system, a recurring challenge for users working across multiple organizations was the friction of session management. To solve this, we recently implemented a seamless way to switch companies directly from the application interface.

The Challenge: Context Switching

Users often manage multiple fleet entities under a single master account. Previously, changing the active context required logging out and re-authenticating, or manually updating local preferences. This created significant overhead and interrupted the user's workflow during critical dispatch or reporting tasks.

The Technical Solution

We leveraged our existing Vue.js frontend to manage the state of the active organization. By utilizing Axios to communicate with our Ruby on Rails backend, we can update the user's current session or JWT claims to reflect the selected entity without forcing a full page reload.

Handling Organization Transitions

In our Vue component, we trigger an API call to update the backend context, subsequently refreshing the application state to ensure data isolation between companies.

async function switchOrganization(orgId: string) {
  try {
    await axios.post('/api/v1/sessions/switch_org', { org_id: orgId });
    // Clear local cache and re-fetch dashboard data
    await fetchFleetOverview();
    window.location.reload();
  } catch (error) {
    console.error('Failed to switch organization', error);
  }
}

This snippet demonstrates the core logic: the frontend communicates the user's intent to the backend. Once the backend persists the new context, the application state is reset to pull data specific to the selected organizational unit.

Why This Matters

Think of this implementation like switching tabs in a browser. Previously, the user was forced to close the entire browser window just to navigate to a different site. By internalizing the switch, we keep the user within the same 'workspace' while dynamically changing the underlying data stream.

Key Takeaway

When designing multi-tenant systems, prioritize making the 'tenant context' a first-class citizen of your state management. If your users frequently switch contexts, build the mechanism to be as low-friction as possible—ideally a single API interaction that refreshes your state containers.


Generated with Gitvlg.com

Streamlining Multi-Tenancy: Implementing Company Switching in Ryuu-no-Mi
JAIME ANDRÉS MONSERRATE VILLA

JAIME ANDRÉS MONSERRATE VILLA

Author

Share: