-
Notifications
You must be signed in to change notification settings - Fork 430
feat(core): Split setActive into selectSession and selectOrganization #7646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat(core): Split setActive into selectSession and selectOrganization #7646
Conversation
Add two new purpose-specific methods to replace the generic setActive: - selectSession: For session selection (sign-in, sign-up, session switching) - selectOrganization: For organization selection (org switching, personal workspace) Changes: - Add SelectSessionOptions, SelectOrganizationOptions types - Add selectSession and selectOrganization methods to Clerk core - Update React hooks (useSignIn, useSignUp) to return selectSession - Update shared hooks (useSessionList, useOrganizationList) with new methods - Update framework integrations (Next.js, Vue, Expo) - Rename internal hooks: __internal_onBeforeSelectSession, __internal_onAfterSelectSession setActive is currently kept as deprecated for backwards compatibility.
- Remove setActive from UseSignInReturn, UseSignUpReturn, UseSessionListReturn, UseOrganizationListReturn types - Update React hooks (useSignIn, useSignUp) to return only selectSession - Update shared hooks (useSessionList, useOrganizationList) to return only selectSession/selectOrganization - Update Vue composables (useSignIn, useSignUp, useSessionList) to return only selectSession - Update Expo hooks (useSSO, useOAuth, useSignInWithApple) to return only selectSession - Update UI components to use selectOrganization from useOrganizationList - Rename useSetSessionWithTimeout to useSelectSessionWithTimeout
🦋 Changeset detectedLatest commit: a6880dd The changes in this PR will be included in the next version bump. This PR includes changesets to release 20 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
@clerk/agent-toolkit
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/dev-cli
@clerk/expo
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/localizations
@clerk/nextjs
@clerk/nuxt
@clerk/react
@clerk/react-router
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/ui
@clerk/upgrade
@clerk/vue
commit: |
📝 WalkthroughWalkthroughThis pull request replaces the 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@packages/clerk-js/src/core/clerk.ts`:
- Around line 1658-1673: The setActiveNavigate wrapper currently uses
this.organization when the external `organization` config is a string, which can
be stale; instead resolve the selected org from the activating session before
calling `navigate`. In the async function `setActiveNavigate` (wrapping
`navigate` and using `SessionResource`), if `organization === null` pass null;
if `typeof organization === 'string'` derive the org from the provided `session`
(prefer `session.lastActiveOrganizationId` or fall back to finding a membership
with matching organization id in `session.memberships`) and pass that resolved
id/object to `navigate`; otherwise pass the `organization` object directly.
Update the resolution logic in `setActiveNavigate` to use
`session.lastActiveOrganizationId`/`session.memberships` instead of
`this.organization` before invoking `navigate` and handling string return values
with `this.navigate`.
In `@packages/expo/src/hooks/useSignInWithGoogle.types.ts`:
- Around line 7-10: The implementation in useSignInWithGoogle.ts returns/exports
a field named setActive while the public type
StartGoogleAuthenticationFlowReturnType expects selectSession; update the
implementation to return selectSession (rename setActive to selectSession where
it is constructed/returned) or re-export StartGoogleAuthenticationFlowReturnType
from useSignInWithGoogle.ts so there’s a single source of truth; ensure the
function that builds the return object (the startGoogleAuthenticationFlow / hook
return value) and any local type aliases are updated to use selectSession and
the SelectSessionHook symbol.
| // Convert navigate callback to setActive format if provided | ||
| const setActiveNavigate = navigate | ||
| ? async ({ session }: { session: SessionResource }) => { | ||
| // Resolve the organization for the callback | ||
| const org = | ||
| organization === null | ||
| ? null | ||
| : typeof organization === 'string' | ||
| ? (this.organization ?? null) | ||
| : organization; | ||
|
|
||
| const result = await navigate({ session, organization: org }); | ||
| if (typeof result === 'string') { | ||
| await this.navigate(result); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the selected organization when invoking navigate.
When organization is a string, the callback receives this.organization (the current org), not the newly selected one. This can route to the wrong org or personal workspace. Resolve the organization from the session being activated (e.g., via session.lastActiveOrganizationId or memberships) before calling navigate.
🛠️ Proposed fix
- const org =
- organization === null
- ? null
- : typeof organization === 'string'
- ? (this.organization ?? null)
- : organization;
+ const org =
+ organization === null
+ ? null
+ : typeof organization === 'string'
+ ? session.user.organizationMemberships.find(
+ mem => mem.organization.id === session.lastActiveOrganizationId,
+ )?.organization ?? null
+ : organization;🤖 Prompt for AI Agents
In `@packages/clerk-js/src/core/clerk.ts` around lines 1658 - 1673, The
setActiveNavigate wrapper currently uses this.organization when the external
`organization` config is a string, which can be stale; instead resolve the
selected org from the activating session before calling `navigate`. In the async
function `setActiveNavigate` (wrapping `navigate` and using `SessionResource`),
if `organization === null` pass null; if `typeof organization === 'string'`
derive the org from the provided `session` (prefer
`session.lastActiveOrganizationId` or fall back to finding a membership with
matching organization id in `session.memberships`) and pass that resolved
id/object to `navigate`; otherwise pass the `organization` object directly.
Update the resolution logic in `setActiveNavigate` to use
`session.lastActiveOrganizationId`/`session.memberships` instead of
`this.organization` before invoking `navigate` and handling string return values
with `this.navigate`.
| export type StartGoogleAuthenticationFlowReturnType = { | ||
| createdSessionId: string | null; | ||
| setActive?: SetActive; | ||
| selectSession?: SelectSessionHook; | ||
| signIn?: SignInResource; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Align runtime return shape with renamed field
StartGoogleAuthenticationFlowReturnType now exposes selectSession (Line 9), but packages/expo/src/hooks/useSignInWithGoogle.ts still defines the return type with setActive (lines 10–15 in that file). If the implementation still returns setActive, consumers will see selectSession as undefined or hit type/runtime mismatches. Please update the implementation (and its local type definition if still present) to return selectSession, or re-export this type to keep a single source of truth.
🤖 Prompt for AI Agents
In `@packages/expo/src/hooks/useSignInWithGoogle.types.ts` around lines 7 - 10,
The implementation in useSignInWithGoogle.ts returns/exports a field named
setActive while the public type StartGoogleAuthenticationFlowReturnType expects
selectSession; update the implementation to return selectSession (rename
setActive to selectSession where it is constructed/returned) or re-export
StartGoogleAuthenticationFlowReturnType from useSignInWithGoogle.ts so there’s a
single source of truth; ensure the function that builds the return object (the
startGoogleAuthenticationFlow / hook return value) and any local type aliases
are updated to use selectSession and the SelectSessionHook symbol.
Summary
Splits the
setActivemethod into two purpose-specific methods for Core 3:selectSession(session, options?)- For session selection (sign-in, sign-up, multi-session switching, sign out)selectOrganization(organization, options?)- For organization selection (org switching, personal workspace)This is a breaking change that removes
setActivefrom hook return types entirely (clean break approach).Changes
New Methods on Clerk Object
clerk.selectSession(session, options?)- wraps session-relatedsetActivecallsclerk.selectOrganization(organization, options?)- wraps organization-relatedsetActivecallsHook Return Type Changes
useSignIn()setActiveselectSessionuseSignUp()setActiveselectSessionuseSessionList()setActiveselectSessionuseOrganizationList()setActiveselectOrganizationInternal Window Hooks (Next.js)
__internal_onBeforeSetActive→__internal_onBeforeSelectSession__internal_onAfterSetActive→__internal_onAfterSelectSessionMigration Example
Before:
After:
Packages Affected
@clerk/clerk-js@clerk/shared@clerk/react@clerk/nextjs@clerk/vue@clerk/expo@clerk/uiTest Plan
Linear
Closes USER-4041
Summary by CodeRabbit
Breaking Changes
setActivemethod; replaced withselectSession()andselectOrganization()methods.New Features
selectSession()andselectOrganization()methods now support optional navigation options including redirectUrl and navigate callbacks.useSignIn,useSignUp, anduseSessionListnow returnselectSession;useOrganizationListreturnsselectOrganization.✏️ Tip: You can customize this high-level summary in your review settings.