Switch Active Tenant From Your App

This guide explains how to switch the active tenant for a user who is subscribed to multiple tenants. This feature applies only to users who have been both added to and confirmed for more than one tenant.

Overview

Frontegg offers the ability to change which tenant is the active tenant for a user who is subscribed to multiple tenants. The active tenant is the tenant the user is logged into each time the user logs into your application. Once the user is logged into your application, you can allow them to switch their active tenant and therefore switch the tenant they are logged into.

One way to implement this is to allow a user to select their own active tenant. For instance, you can present to the user a list of the tenants they are subscribed to. Then, based on which tenant the user selects, call the switch tenant function with the tenant id from the user's selected tenant.

Frontegg offers this feature in React, Angular, and Vue. Regardless of which you use, you need access to the user object and the function that sets the active tenant.

Read below to learn how to implement this.

Code Examples

To implement this feature, you need the user object and the function that switches the active tenant.

You need the user object because it gives you a list of the tenants the user is subscribed to. The list of tenants is user.tenantIds.

You need the function that switches the active tenant in order to set one of user's tenants as the active tenant. That function is called switchTenant and requires one argument in the following format: switchTenant({tenantId: 'new-tenant-id'}).

πŸ“˜

A note on tenantId

Instead of 'new-tenant-id', use the tenantId for the tenant that you want to become the active tenant. Note that the tenantId that you set as the active tenant needs to be a tenant that the user actually belongs to.

React

To access the user object and the switchTenant function, import useAuth and useAuthActions from @frontegg/react.

Use the useAuth hook to get the user object and the useAuthActions hook to access the switchTenant function.

Here is an example:

import { useAuth, useAuthActions } from "@frontegg/react";

function App() {
  const { switchTenant } = useAuthActions();
  const { user } = useAuth();

  // Use user.tenantIds to get the tenants the user is subscribed to

  const handleSwitchTenant = () => { 
      switchTenant({ tenantId: 'new-tenant-id' });
  };
  
  return <button onClick={handleSwitchTenant}>Select Active Tenant</button>;
}

export default App;

Angular

Inject the FronteggAppAuthService into your application. This service gives you the user object and the switchTenant function.

Here is an example:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
  authenticated?: boolean;
  user?: any;

  constructor(
    private fronteggAppAuthService: FronteggAppAuthService,
    private fronteggAppService: FronteggAppService
  ) {}

  ngOnInit(): void {
    this.fronteggAppAuthService?.isAuthenticated$.subscribe(
      (isAuthenticated) => {
        this.authenticated = isAuthenticated;
      }
    );
    this.fronteggAppAuthService?.userState$.subscribe((user) => {
      this.user = user;
    });
  }

  public showApp(): void {
    this.fronteggAppService?.showAdminPortal();
  }

  public switchTenant(): void {
    // Use this.user.tenantIds to get the tenants the user is subscribed to
    this.fronteggAppAuthService.switchTenant({tenantId: 'new-tenant-id'})
  }
}
<frontegg-app>
    <div *ngIf="authenticated">
      <img src="{{ user?.profilePictureUrl }}" alt="{{ user?.name }}" />
      <div>User name: {{ user?.name }}</div>
      <div><button (click)="showApp()">Admin Panel</button></div>
      <div><button (click)="switchTenant()">Switch Tenant</button></div>
    </div>
    <router-outlet></router-outlet>
  </frontegg-app>

Vue

Use authState to access the user object and the switchTenant function. Because it is required as part of the generic frontegg/Vue.js setup, you already have access to authState from the mapAuthState method.

Here is an example:

<template>
  <div id="app" v-if="fronteggLoaded">
    <img alt="My logo" src="./assets/logo.png" />
    <HelloWorld msg="Welcome to Your Vue.js with Frontegg App" />
    <p>
      Logged in as
      {{ this.authState.user ? this.authState.user.email : "Not logged in" }}
    </p>
    <button v-on:click="showAdminPortal">Open admin portal</button>
    <button v-on:click="switchTenant">Switch Tenant</button>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";
import { AdminPortal } from "@frontegg/vue";

export default {
  name: "App",
  components: {
    HelloWorld,
  },
  data() {
    return {
      ...this.mapAuthState(),
    };
  },
  methods: {
    showAdminPortal() {
      AdminPortal.show();
    },
    switchTenant() {
      // Use this.authState.user.tenantIds to get the tenants the user is subscribed to
      this.authState.switchTenant({ tenantId: 'new-tenant-id' });
    },
  },
};
</script>

Did this page help you?