useOrganizations
useOrganizations() exposes the organizations the signed-in user belongs
to, the currently-active organization (the JWT org_id claim), and the
imperative helpers to create and switch organizations. It reads from the
cached boot payload on <ToriiProvider>, so there’s no extra round-trip on
render.
This is the data layer behind <OrganizationList>.
Use it directly when you need a custom org surface (e.g. your own switcher
in a navbar).
import { useOrganizations } from '@torii-js/torii-react';
function OrgSwitcher() { const { organizations, activeOrganizationId, switchActiveOrganization } = useOrganizations();
return ( <select value={activeOrganizationId ?? ''} onChange={(e) => switchActiveOrganization(e.target.value)} > {organizations.map((org) => ( <option key={org.id} value={org.id}> {org.name} </option> ))} </select> );}Returns
Section titled “Returns”| Field | Type | Description |
|---|---|---|
organizations | ClientOrganizationSummary[] | Orgs the user belongs to ({ id, name, role, role_name? }). role_name is the human-readable display name to show; fall back to role when it’s absent. |
activeOrganizationId | string | null | The active org id from the JWT org_id claim, or null when none is active. |
activeOrganization | ClientOrganizationSummary | null | The active org summary, or null. |
createOrganization | (name: string) => Promise<ClientOrganizationSummary> | Creates an org and resolves to its summary. Rejects on failure. |
switchActiveOrganization | (organizationId: string) => Promise<void> | Switches the active org: re-mints the access token with the new org_id claim and persists the choice server-side, so subsequent session refreshes retain it. |
Behaviour
Section titled “Behaviour”- Active org is derived from the JWT. When
activeOrganizationIdisnull, no org is active, so prompt the user to pick one (e.g. render<OrganizationList>). switchActiveOrganizationpersists. The choice is stored on the session row, so a reload re-mints the token with the same active org.- Must be called inside a
<ToriiProvider>.