Skip to main content

Organization switcher

A menu that shows the customer organization that a person works in, and lets the person change it.

PatternReadyView source
Works with
Reactshadcn/ui
Runs on sample data, with the neutral theme of shadcn/ui.

For products with business customers. A person who belongs to several customer organizations chooses one, and the app signs in again for it. Open the menu in the preview.

Installation​

The command adds the file to your project, with the shadcn/ui components that it uses. Your project needs shadcn/ui.

pnpm dlx shadcn@latest add https://examples.lumoauth.dev/r/organization-switcher.json

Usage​

Choosing an organization calls onSwitch. Start sign-in again there, with the organization parameter.

components/header-organization.tsx
'use client';

import { OrganizationSwitcher, type CustomerOrganization } from '@/components/organization-switcher';

export function HeaderOrganization({ organizations, current, loginUrl, portalUrl }: {
organizations: CustomerOrganization[];
current: string;
loginUrl: string; // your own login route
portalUrl: string; // https://app.lumoauth.dev/b2b/{orgId}
}) {
return (
<OrganizationSwitcher
organizations={organizations}
current={current}
onSwitch={(slug) => window.location.assign(`${loginUrl}?organization=${encodeURIComponent(slug)}`)}
portalHref={(slug) => `${portalUrl}/${slug}/`}
/>
);
}

Your login route passes organization on to the authorization request. With one organization and nothing else to choose, the pattern renders a label and no menu. "Manage" opens the hosted customer admin portal, and appears for the roles in adminRoles.

Where the data comes from​

What the pattern showsCall
The organizations of a personAdmin API, customer organizations, or the hosted page /account/organizations
The organization of the sessionThe organization claims of the token
Change itSign-in with the organization parameter
The portal of an organization/b2b/{orgId}/{organizationSlug}/

States​

Runs on sample data, with the neutral theme of shadcn/ui.
  • One organization. A label, with no menu.
  • Several organizations, as a member: no "Manage" item.

Props​

PropTypeDefaultDescription
organizationsrequiredCustomerOrganization[]The organizations of the person: slug, name, role and logoUrl.
currentrequiredstringThe slug of the organization that the session is for.
onSwitch(slug: string) => voidStart sign-in again for another organization.
pendingInvitationsnumber0The number of invitations that wait for an answer.
onOpenInvitations() => voidCalled for the "Invitations" item.
portalHref(slug: string) => stringBuilds the link to the customer admin portal.
adminRolesstring[]['owner', 'admin']The roles that may open the portal.
classNamestringClass name for the trigger.