Skip to content
Torii docs

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>
);
}
FieldTypeDescription
organizationsClientOrganizationSummary[]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.
activeOrganizationIdstring | nullThe active org id from the JWT org_id claim, or null when none is active.
activeOrganizationClientOrganizationSummary | nullThe 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.
  • Active org is derived from the JWT. When activeOrganizationId is null, no org is active, so prompt the user to pick one (e.g. render <OrganizationList>).
  • switchActiveOrganization persists. 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>.