Skip to main content

Switch Organizations

Cloud has the newest features. Some may not be in a release yet; the newest release is Identity & Access Management 7.3.0.

Let a person who belongs to several organizations move between them. A Wayfinder Enterprise travel consultant has one account that lives in the platform organization and is shared into Northstar Labs and Initech. They book trips for Northstar in the morning and for Initech in the afternoon, and every token they hold names exactly one of those organizations.

Prerequisites​

  • The platform organization and the shared application from Wayfinder Enterprise sample.
  • A person shared into more than one organization. See Invite People In.
  • To switch without asking for the password again, a sign-in flow that keeps a single sign-on (SSO) session. The Basic + Enterprise SSO template keeps none, so add Check SSO Session and Save / Load Session steps to it. See Organization Units and Single Sign-On for where they go around Resolve OU.

How a Switch Works​

Each organization is an organization unit (OU), a node in the hierarchy that owns its own users, groups, and roles. See Manage Organization Units. A sign-in lands in exactly one OU, and its tokens name that OU. A switch is a new sign-in bounded to another OU. When the sign-in flow keeps an SSO session and the person can sign in to the other OU, Choreo Identity & Access Management reuses that session and asks for no credentials. Otherwise, the person signs in again.

When your application signs people in by redirecting them to Choreo Identity & Access Management, the SDK tries the quietest way first:

  • A device secret. A session holds one when the sign-in asked for the device_sso scope. The SDK exchanges it for tokens at the other OU's token endpoint, with no browser round trip. The application needs the token exchange grant for this. See Sign-In With a Device Secret.
  • A silent sign-in. Without a device secret, the SDK asks for a sign-in in a hidden frame with prompt=none, which tells Choreo Identity & Access Management to answer at once from the existing session, never by showing a screen.
  • A full-page redirect. When neither works, the browser goes to the other OU's sign-in, where the person signs in again.

The same order, when the consultant picks Initech:

A failed switch leaves the previous session as it was.

When Your Application Signs In Another Way​

The order above applies when your application redirects people to Choreo Identity & Access Management to sign in. The SDKs switch differently in the other sign-in modes:

  • App-native sign-in. When your application renders the sign-in screens itself, the SDK starts the sign-in flow again with the other OU's ID. It sends the SSO session handles that earlier flow steps returned, so Choreo Identity & Access Management can reuse the session without asking for credentials. When the flow needs the person, the web SDKs fail the switch with InteractionRequired, so render the sign-in for that OU instead. The iOS and Android SDKs return the flow step for you to continue.
  • Sign-in through your own backend. When your backend holds the tokens (endpoints.backend), the SDK asks the backend to switch. It sends POST {ouSwitch}?ouId=<ou-id> with {"ouId": "<ou-id>"} as a JSON body. ouSwitch defaults to /api/auth/switch. The backend switches the session it holds, for example by exchanging its device secret at the other OU's token endpoint, and answers 200. The SDK then reads the session again without leaving the page. A 401 with session_expired makes the SDK refresh the backend session once and try the switch again. When the backend answers that the person has to sign in (interaction_required, login_required, consent_required, or account_selection_required), or answers 401 again, the SDK opens the backend's sign-in for that OU instead. A login_required or a remaining 401 means the backend's session has ended, so the SDK also treats the person as signed out. With {redirect: false} in the web SDKs, or without a browser handler in the iOS and Android SDKs, the switch fails instead.

The iOS and Android SDKs take the first way that applies: your backend, then a device secret, then the browser, and then the app-native sign-in flow.

List the Organizations a Person Can Access​

GET /users/me/ous lists the OUs the signed-in person can access: the one they live in and every one they are shared into. It needs no management permission, only the person's own access token. current is the OU the token was issued for. https://<DATA_PLANE_URL> is your environment's data plane URL, shown on the environment's Endpoints page. To open it, select your project, go to Deployment pipeline, and click Open environment on the environment's card.

curl 'https://<DATA_PLANE_URL>/users/me/ous?scope=siblings' \
-H 'Authorization: Bearer <access-token>'
{
"current": "7b6a5c4d-3e2f-4a1b-8c9d-0e1f2a3b4c5d",
"totalResults": 2,
"startIndex": 1,
"count": 2,
"organizationUnits": [
{"id": "7b6a5c4d-3e2f-4a1b-8c9d-0e1f2a3b4c5d", "handle": "northstar", "name": "Northstar Labs"},
{"id": "9e8d7c6b-5a4f-4e3d-2c1b-0a9f8e7d6c5b", "handle": "initech", "name": "Initech"}
],
"links": []
}

Without scope, the list also includes the OU the consultant lives in, Wayfinder Enterprise. scope=siblings keeps only the accessible OUs that share the parent of current, which in Wayfinder Enterprise means the companies.

Add the Switcher​

OrganizationUnitSwitcher from @thunderid/react lists the person's organizations and switches on a pick:

src/Header.jsx
import {OrganizationUnitSwitcher, SignedIn, useThunderID} from '@thunderid/react';

export default function Header() {
const {currentOrganizationUnit} = useThunderID();

return (
<SignedIn>
<span>Working in {currentOrganizationUnit?.name}</span>
<OrganizationUnitSwitcher scope="siblings" onError={(error) => console.error(error)} />
</SignedIn>
);
}
PropDescription
scopesiblings lists the accessible organizations that share the parent of the current organization. Omit it to list every accessible organization.
onSwitchCalled with the organization and the refreshed user after a switch that completes in place. A switch that leaves the page completes on the callback instead.
onSelectTakes over a pick. The switcher calls it with the organization instead of switching, so your application decides what happens.
onErrorCalled with the SDK error when a switch or a load fails. The switcher shows the message either way.
switchOptionsOptions passed to switchOrganizationUnit, such as {redirect: false}.
title, className, preferences, childrenPresentation, and a render prop for a fully custom switcher.

currentOrganizationUnit holds the id, handle, and name read from the ouId, ouHandle, and ouName claims of the ID token. It is for display only. Authorization decisions belong to the server, which reads the access token.

Switch From Your Own Interface​

Call switchOrganizationUnit from useThunderID() when a menu, a link, or a route change picks the organization:

src/useOpenOrganization.js
import {OrganizationUnitSwitchErrorCodes, useThunderID} from '@thunderid/react';

export default function useOpenOrganization() {
const {switchOrganizationUnit} = useThunderID();

return async (organizationUnitId) => {
try {
await switchOrganizationUnit(organizationUnitId, {redirect: false});
} catch (error) {
if (error.code === OrganizationUnitSwitchErrorCodes.InteractionRequired) {
// The person has to sign in again. Ask first, then switch with redirects allowed.
}
}
};
}

With redirect: false, a switch that needs the person fails with InteractionRequired instead of leaving the page. Leave it out to fall back to a full-page redirect.

In a Next.js application, call switchOrganizationUnit from useThunderID() too, but leave out {redirect: false} in redirect mode. The @thunderid/nextjs provider runs the switch on the server. When the session holds a device secret, the server exchanges it at the organization's token endpoint and replaces the session without leaving the page. Without a device secret, or when Choreo Identity & Access Management cannot use it or needs the person, the server sends the browser to the organization's authorization endpoint instead, and the callback replaces the session. The device secret stays on the server. With {redirect: false}, a switch that needs the redirect fails with InteractionRequired. In embedded mode, the provider starts the sign-in flow for the organization instead, and a flow that needs the person fails with InteractionRequired. @thunderid/nextjs does not export OrganizationUnitSwitchErrorCodes, so compare error.code with the code values as strings.

In a Nuxt application, switchOrganizationUnit from useThunderID() always sends the browser to the Nuxt server's sign-in route with the organization's ID, and the callback replaces the session. It does not switch silently, and it ignores redirect.

Sign In Straight Into One Organization​

When your application already knows the organization, such as from a company-specific link, bound the first sign-in to it. Pass the organization's ID, not its handle:

  • Redirect-based sign-in: signIn({organizationUnitId}), or <SignInButton signInOptions={{organizationUnitId}} />. The request goes to /ou/{ouId}/oauth2/authorize, and the code is redeemed at /ou/{ouId}/oauth2/token.
  • App-native sign-in: <SignIn organizationUnitId={organizationUnitId} />. The SDK sends ouId on the request that starts the sign-in flow.

To let people type a handle instead, resolve it in the sign-in flow with a Resolve OU step. See Let the User Pick the Organization Unit First.

What Changes After a Switch​

The new tokens describe the person in the organization they switched to:

  • Organization claims. ouId, ouName, and ouHandle name the new organization, not the one the person lives in.
  • Roles. Only role assignments made in the new organization count, and the roles claim lists only those.
  • Groups. The groups claim lists only groups the new organization owns.
  • Refresh. A refresh token checks permissions again in the organization it was issued for, and is refused once the person can no longer sign in there, for example after their share is removed.

The application's token configuration must include these claims. See What the Sign-In Carries.

Handle Switch Errors​

switchOrganizationUnit fails with a code from OrganizationUnitSwitchErrorCodes:

CodeCause
UnauthorizedClientThe organization does not exist, or the application is not shared with it. The server answers both with unauthorized_client, so the two cannot be told apart.
InteractionRequiredThe person has to sign in again, and the switch was not allowed to redirect.
InvalidOrganizationUnitNo organization ID was given.
FailedThe bounded sign-in failed for another reason. The server's OAuth error is the message.

At an OU's own token endpoint, /ou/{ouId}/oauth2/token, an authorization code issued for another OU fails with invalid_grant. A device secret fails with invalid_grant for a person who is not in that OU, and with interaction_required when its session cannot be reused without the person. The OU token endpoint takes no refresh tokens: a refresh goes to /oauth2/token, which refuses it with invalid_grant once the person can no longer sign in to the token's OU.

Next Steps​

  • Invite People In: Share an existing account into another organization, so it shows up in the switcher.
    • B2B Quickstart with React: Put the switcher, the organization context, and organization-aware tokens to work in a new app.
    • Design Decisions: Choose how a sign-in picks the organization in the first place.

Was this page helpful?