Repository navigation
Switch organization units with the device secret in the Next.js and Nuxt SDKs - #180
Merged
AnuradhaSK merged 1 commit intoNov 14, 2026
Merged
Conversation
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
🔀 Cross-SDK feature parity, no ports neededThis pull request is labelled |
brionmario
previously approved these changes
Nov 14, 2026
…uxt SDKs Next.js and Nuxt apps switched organization units only by redirecting the browser to the unit's sign-in and back. When the session holds a device secret, the server now exchanges it at the unit's token endpoint, rewrites the session cookie, and completes the switch without leaving the page. Both fall back to the redirect when there is no device secret, or when the server needs the user or cannot use the secret. Signed-off-by: AnuradhaSK <anuradha199528@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: AnuradhaSK <anuradha199528@gmail.com>
AnuradhaSK
force-pushed
the
nextjs-device-secret-switch
branch
from
November 14, 2026 06:37
00f2cdc to
7964714
Compare
brionmario
approved these changes
Nov 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
In a Next.js or Nuxt app,
OrganizationUnitSwitcherswitches by sending the browser to{baseUrl}/ou/{ouId}/oauth2/authorizeand back to the app. The SSO session spares the user a password, but the page still leaves the app on every switch.PR #160 added a back-channel switch to the browser SDK: a sign-in that asks for the
device_ssoscope returns a device secret, and exchanging it at the unit's token endpoint gets the unit's tokens without a redirect. The exchange itself lives in the core client and supports confidential clients, but only the browser client used it. This PR uses it on the Next.js and Nuxt servers, where the device secret stays in the server-side session store and never reaches the browser.Approach
@thunderid/nodeThunderIDNodeClient.switchOrganizationUnitWithDeviceSecret(organizationUnitId, sessionId)is now public. It runs the core exchange for the session and, on success, indexes the session for back-channel logout, as the redirect switch's callback does.@thunderid/javascriptINTERACTION_ERRORS,UNUSABLE_DEVICE_SECRET_ERRORS) move to the core asOrganizationUnitSwitchFallbackErrors, which the browser client, the Next.js action and the Nuxt helper all read.@thunderid/nextjsswitchOrganizationUnitActiontries the device secret first.{switched: true}. The provider already handles that result (it rereads the user and the current unit and refreshes the router), so it needs no change.redirect: falsestill fails withinteraction_required.unauthorized_clientfails withUnauthorizedClient, and any other error withFailed, keeping the session.handleOAuthCallbackActionintoutils/setSessionCookie.ts, so the callback and the switch share it. The callback's behaviour is unchanged.@thunderid/nuxtPOST /api/auth/switch(NuxtAPIRoutes.SWITCH). It follows theouSwitchcontract the browser SDK's backend mode already defines: the unit in the query and in a{"ouId"}body, the new state on success, and an OAutherroron a refusal.issueSessionCookieand answers with the new auth state, including the organization unit.401withlogin_required. No device secret answers400withinteraction_required. A token endpoint error is passed on with400, and a failed request answers502withserver_error. The session stays as it was in each case.switchOrganizationUnitin the plugin calls the route throughutils/switchOrganizationUnitOnServer.ts.refreshNuxtData(), so pages that fetch session data reload it, and resolves with the user.redirect: falseit fails withInteractionRequiredinstead.unauthorized_clientfails withUnauthorizedClient, and any other refusal withFailed.OrganizationUnitSwitchermarks the new unit after a switch that stays on the page, and ends its busy state on success as well as on failure, as the Vue SDK's switcher does.To get the in-place switch, an app adds
device_ssoto its scopes, and its ThunderID application allows theurn:ietf:params:oauth:grant-type:token-exchangegrant. Apps that don't keep the redirect switch.The SDK development specification still lists organization operations as a future target, so this PR, like #160, adds no specification text.
Related Issues
Related PRs
Checklist
device_ssoadded to the apps' scopes. A user shared into three companies signed in to Initech, then switched to Umbrella and to Globex fromOrganizationUnitSwitcher.http://localhost:3000with no navigation to ThunderID and no password prompt.OrganizationUnitSwitcherand a server route that reads the session's access token): each switch made no navigation at all.Memberwithoutorgin Umbrella,Adminwithorgin Globex).device_sso: the switch went through ThunderID's sign-in and back, with no password prompt, and landed in Umbrella.packages/node/src/__tests__/ThunderIDNodeClient.organizationUnit.test.ts: exchange at the unit token endpoint with client authentication, tokens replaced, device secret kept, session indexed; no device secret; a refused secret dropped with the session kept.packages/nextjs/src/server/actions/auth/__tests__/switchOrganizationUnitAction.test.ts: in-place switch with the cookie rewritten; fallback oninteraction_required,login_required,invalid_grantandinvalid_request;redirect: false;unauthorized_clientand other errors; a failed request.packages/nextjs/src/utils/__tests__/setSessionCookie.test.tspackages/nuxt/tests/unit/switch-post.test.ts: in-place switch with the cookie rewritten and the new state returned; the unit from the body; a missing unit; no session; no device secret; a token endpoint error; a failed request.packages/nuxt/tests/unit/switch-organization-unit-on-server.test.ts: the new state; a sign-in on each fallback error and on401;UnauthorizedClientandFailed.packages/nuxt/tests/unit/organization-unit-switcher.test.ts: the new unit marked and the busy state ended, on success and on failure.breaking changelabel added.parity/prs-raisedorparity/prs-not-neededadded.parity/prs-raised, the port links are posted as a reply on the parity check's comment.SDK parity
switchOrganizationUnit(_:options:)already exchanges the device secret before falling back to the browserswitchOrganizationUnitalready exchanges the device secret before falling back to the browserWithin this repository,
@thunderid/expresshas no organization unit switch, so there is nothing to back with a device secret there.Security checks
The device secret stays in the server's session store: the Next.js and Nuxt SDKs both use the Node client's in-memory
MemoryCacheStore, keyed by the session ID. It is not added to either session cookie, which carry the access and refresh tokens as before (HttpOnly and signed). The exchange authenticates with the client secret.The store is per process. After a restart, or when a switch reaches another instance than the one that handled the sign-in, the session holds no device secret and the switch falls back to the redirect. Nuxt rebuilds a session from its cookie after a restart, and a rebuilt session has no device secret for the same reason.
🤖 Generated with Claude Code