Organization switcher
A menu that shows the customer organization that a person works in, and lets the person change it.
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
- CLI
- Manual
The command adds the file to your project, with the shadcn/ui components that it uses. Your project needs shadcn/ui.
- pnpm
- npm
- yarn
- bun
pnpm dlx shadcn@latest add https://examples.lumoauth.dev/r/organization-switcher.json
npx shadcn@latest add https://examples.lumoauth.dev/r/organization-switcher.json
yarn dlx shadcn@latest add https://examples.lumoauth.dev/r/organization-switcher.json
bunx --bun shadcn@latest add https://examples.lumoauth.dev/r/organization-switcher.json
Add the shadcn/ui components that the pattern uses.
npx shadcn@latest add avatar badge button dropdown-menuInstall the packages.
npm install lucide-reactCopy the code into your project.
components/organization-switcher.tsx'use client';import * as React from 'react';import { Check, ChevronsUpDown, Mail, Settings2 } from 'lucide-react';import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';import { Badge } from '@/components/ui/badge';import { Button } from '@/components/ui/button';import {DropdownMenu,DropdownMenuContent,DropdownMenuGroup,DropdownMenuItem,DropdownMenuLabel,DropdownMenuSeparator,DropdownMenuTrigger,} from '@/components/ui/dropdown-menu';import { cn } from '@/lib/utils';export interface CustomerOrganization {/** The slug of the customer organization. Pass it as `organization` at sign-in. */slug: string;name: string;/** The role of the person in this organization. */role?: string;logoUrl?: string;}export interface OrganizationSwitcherProps {organizations: CustomerOrganization[];/** The slug of the organization that the session is for. */current: string;/** Start sign-in again for another organization. */onSwitch?: (slug: string) => void;/** The number of invitations that wait for an answer. */pendingInvitations?: number;onOpenInvitations?: () => void;/** Builds the link to the customer admin portal. Shown to people who administer the organization. */portalHref?: (slug: string) => string;/** The roles that may open the portal. */adminRoles?: string[];className?: string;}function OrgAvatar({ org, className }: { org: CustomerOrganization; className?: string }) {const initials = org.name.split(/\s+/).slice(0, 2).map((word) => word[0]).join('').toUpperCase();return (<Avatar className={cn('size-6 rounded-md', className)}>{org.logoUrl && <AvatarImage src={org.logoUrl} alt="" />}<AvatarFallback className="rounded-md text-[0.625rem] font-semibold">{initials}</AvatarFallback></Avatar>);}export function OrganizationSwitcher({organizations,current,onSwitch,pendingInvitations = 0,onOpenInvitations,portalHref,adminRoles = ['owner', 'admin'],className,}: OrganizationSwitcherProps) {const active = organizations.find((org) => org.slug === current) ?? organizations[0];if (!active) return null;const administers = Boolean(active.role && adminRoles.includes(active.role.toLowerCase()));// One organization and nothing else to choose: a label, not a menu.if (organizations.length === 1 && pendingInvitations === 0 && !(portalHref && administers)) {return (<span className={cn('inline-flex items-center gap-2 text-sm font-medium', className)}><OrgAvatar org={active} />{active.name}</span>);}return (<DropdownMenu><DropdownMenuTrigger className={cn('rounded-md outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50', className)}><span className="inline-flex h-9 w-56 items-center gap-2 rounded-md border bg-background px-2 text-sm shadow-xs hover:bg-accent"><OrgAvatar org={active} /><span className="min-w-0 flex-1 truncate text-left font-medium">{active.name}</span>{pendingInvitations > 0 && <span className="size-2 rounded-full bg-amber-500" aria-label={`${pendingInvitations} invitations`} />}<ChevronsUpDown className="size-4 shrink-0 text-muted-foreground" aria-hidden="true" /></span></DropdownMenuTrigger><DropdownMenuContent align="start" className="w-64">{/* The label stands inside a group: the Base UI menu needs that, and the Radix menu allows it. */}<DropdownMenuGroup><DropdownMenuLabel className="text-xs font-normal text-muted-foreground">Your organizations</DropdownMenuLabel>{organizations.map((org) => (<DropdownMenuItem key={org.slug} onClick={() => org.slug !== active.slug && onSwitch?.(org.slug)}><OrgAvatar org={org} /><span className="min-w-0 flex-1 truncate">{org.name}</span>{org.role && <span className="text-xs text-muted-foreground capitalize">{org.role}</span>}{org.slug === active.slug && <Check className="text-foreground" aria-label="Current" />}</DropdownMenuItem>))}</DropdownMenuGroup>{pendingInvitations > 0 && (<><DropdownMenuSeparator /><DropdownMenuItem onClick={() => onOpenInvitations?.()}><Mail aria-hidden="true" /><span className="flex-1">Invitations</span><Badge variant="secondary" className="tabular-nums">{pendingInvitations}</Badge></DropdownMenuItem></>)}{portalHref && administers && (<><DropdownMenuSeparator /><DropdownMenuItem onClick={() => window.location.assign(portalHref(active.slug))}><Settings2 aria-hidden="true" />Manage {active.name}</DropdownMenuItem></>)}</DropdownMenuContent></DropdownMenu>);}/** A plain button for a page that has no room for the menu. */export function SwitchOrganizationButton({ onClick, className }: { onClick?: () => void; className?: string }) {return (<Button variant="outline" size="sm" onClick={onClick} className={className}><ChevronsUpDown aria-hidden="true" />Switch organization</Button>);}Change the import paths if your project keeps its components in another place.
Usage
Choosing an organization calls onSwitch. Start sign-in again there, with the organization parameter.
'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 shows | Call |
|---|---|
| The organizations of a person | Admin API, customer organizations, or the hosted page /account/organizations |
| The organization of the session | The organization claims of the token |
| Change it | Sign-in with the organization parameter |
| The portal of an organization | /b2b/{orgId}/{organizationSlug}/ |
States
- One organization. A label, with no menu.
- Several organizations, as a member: no "Manage" item.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
organizationsrequired | CustomerOrganization[] | The organizations of the person: slug, name, role and logoUrl. | |
currentrequired | string | The slug of the organization that the session is for. | |
onSwitch | (slug: string) => void | Start sign-in again for another organization. | |
pendingInvitations | number | 0 | The number of invitations that wait for an answer. |
onOpenInvitations | () => void | Called for the "Invitations" item. | |
portalHref | (slug: string) => string | Builds the link to the customer admin portal. | |
adminRoles | string[] | ['owner', 'admin'] | The roles that may open the portal. |
className | string | Class name for the trigger. |