From 81c059751d6c1d938dabfb3634c46efdb8d86cae Mon Sep 17 00:00:00 2001 From: Laura Beatris <48022589+LauraBeatris@users.noreply.github.com> Date: Tue, 25 Nov 2025 11:01:26 -0300 Subject: [PATCH] Do not open prompt when task is pending --- .changeset/afraid-women-buy.md | 7 ++ .../clerk-js/src/core/__tests__/clerk.test.ts | 67 +++++++++++++++++++ packages/clerk-js/src/core/clerk.ts | 6 +- 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 .changeset/afraid-women-buy.md diff --git a/.changeset/afraid-women-buy.md b/.changeset/afraid-women-buy.md new file mode 100644 index 00000000000..d4876c5f31a --- /dev/null +++ b/.changeset/afraid-women-buy.md @@ -0,0 +1,7 @@ +--- +'@clerk/clerk-js': patch +--- + +Prevent enable organization prompt from appearing if there is a session with a pending `choose-organization` task. + +This resolves an issue where, after organizations are enabled via the Dashboard, cached environment resources may cause the prompt to show again when the user is redirected to complete the `choose-organization` task. diff --git a/packages/clerk-js/src/core/__tests__/clerk.test.ts b/packages/clerk-js/src/core/__tests__/clerk.test.ts index d93d9bbfc20..ccf7ca9a091 100644 --- a/packages/clerk-js/src/core/__tests__/clerk.test.ts +++ b/packages/clerk-js/src/core/__tests__/clerk.test.ts @@ -2510,6 +2510,11 @@ describe('Clerk singleton', () => { }); describe('__internal_attemptToEnableEnvironmentSetting', () => { + afterEach(() => { + mockEnvironmentFetch.mockReset(); + mockClientFetch.mockReset(); + }); + describe('for organizations', () => { it('does not open prompt if organizations is enabled in development', async () => { mockEnvironmentFetch.mockReturnValue( @@ -2630,6 +2635,68 @@ describe('Clerk singleton', () => { expect(result?.isEnabled).toBe(false); expect(__internal_openEnableOrganizationsPromptSpy).not.toHaveBeenCalled(); }); + + // Handles case where environment gets enabled via BAPI, but it gets cached and the user is redirected to the choose-organization task + // The enable org prompt should not appear in the task screen since orgs have already been enabled + it('does not open prompt if organizations is disabled in development and session has choose-organization task', async () => { + const mockSession = { + id: '1', + remove: vi.fn(), + status: 'pending', + user: {}, + touch: vi.fn(() => Promise.resolve()), + getToken: vi.fn(), + lastActiveToken: { getRawString: () => 'mocked-token' }, + tasks: [{ key: 'choose-organization' }], + currentTask: { key: 'choose-organization' }, + reload: vi.fn(() => + Promise.resolve({ + id: '1', + status: 'pending', + user: {}, + tasks: [{ key: 'choose-organization' }], + currentTask: { + key: 'choose-organization', + }, + }), + ), + }; + + mockEnvironmentFetch.mockReturnValue( + Promise.resolve({ + userSettings: mockUserSettings, + displayConfig: mockDisplayConfig, + isSingleSession: () => false, + isProduction: () => false, + isDevelopmentOrStaging: () => true, + organizationSettings: { + enabled: false, + }, + }), + ); + + mockClientFetch.mockReturnValue( + Promise.resolve({ + signedInSessions: [mockSession], + }), + ); + + const sut = new Clerk(developmentPublishableKey); + + const __internal_openEnableOrganizationsPromptSpy = vi.fn(); + sut.__internal_openEnableOrganizationsPrompt = __internal_openEnableOrganizationsPromptSpy; + + await sut.load(); + + const result = await sut.__internal_attemptToEnableEnvironmentSetting({ + for: 'organizations', + caller: 'OrganizationSwitcher', + }); + + // Contains the organization task, so the prompt should not be opened + expect(result?.isEnabled).toBe(true); + expect(__internal_openEnableOrganizationsPromptSpy).not.toHaveBeenCalled(); + }); }); }); }); diff --git a/packages/clerk-js/src/core/clerk.ts b/packages/clerk-js/src/core/clerk.ts index 1a5d83aee14..da8f4f62bb6 100644 --- a/packages/clerk-js/src/core/clerk.ts +++ b/packages/clerk-js/src/core/clerk.ts @@ -761,7 +761,11 @@ export class Clerk implements ClerkInterface { switch (setting) { case 'organizations': { - const isSettingDisabled = disabledOrganizationsFeature(this, this.environment); + const isSettingDisabled = + disabledOrganizationsFeature(this, this.environment) && + // Handles case where environment gets enabled via BAPI, but it gets cached and the user is redirected to the choose-organization task + // The enable org prompt should not appear in the task screen since orgs have already been enabled + this.session?.currentTask?.key !== 'choose-organization'; if (!isSettingDisabled) { return { isEnabled: true };