Skip to content

Commit b0846e9

Browse files
authored
fix(clerk-js): Do not open orgs prompt when choose-organization task is pending (#7307)
1 parent b248601 commit b0846e9

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

.changeset/afraid-women-buy.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Prevent enable organization prompt from appearing if there is a session with a pending `choose-organization` task.
6+
7+
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.

packages/clerk-js/src/core/__tests__/clerk.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,11 @@ describe('Clerk singleton', () => {
25102510
});
25112511

25122512
describe('__internal_attemptToEnableEnvironmentSetting', () => {
2513+
afterEach(() => {
2514+
mockEnvironmentFetch.mockReset();
2515+
mockClientFetch.mockReset();
2516+
});
2517+
25132518
describe('for organizations', () => {
25142519
it('does not open prompt if organizations is enabled in development', async () => {
25152520
mockEnvironmentFetch.mockReturnValue(
@@ -2630,6 +2635,68 @@ describe('Clerk singleton', () => {
26302635
expect(result?.isEnabled).toBe(false);
26312636
expect(__internal_openEnableOrganizationsPromptSpy).not.toHaveBeenCalled();
26322637
});
2638+
2639+
// Handles case where environment gets enabled via BAPI, but it gets cached and the user is redirected to the choose-organization task
2640+
// The enable org prompt should not appear in the task screen since orgs have already been enabled
2641+
it('does not open prompt if organizations is disabled in development and session has choose-organization task', async () => {
2642+
const mockSession = {
2643+
id: '1',
2644+
remove: vi.fn(),
2645+
status: 'pending',
2646+
user: {},
2647+
touch: vi.fn(() => Promise.resolve()),
2648+
getToken: vi.fn(),
2649+
lastActiveToken: { getRawString: () => 'mocked-token' },
2650+
tasks: [{ key: 'choose-organization' }],
2651+
currentTask: { key: 'choose-organization' },
2652+
reload: vi.fn(() =>
2653+
Promise.resolve({
2654+
id: '1',
2655+
status: 'pending',
2656+
user: {},
2657+
tasks: [{ key: 'choose-organization' }],
2658+
currentTask: {
2659+
key: 'choose-organization',
2660+
},
2661+
}),
2662+
),
2663+
};
2664+
2665+
mockEnvironmentFetch.mockReturnValue(
2666+
Promise.resolve({
2667+
userSettings: mockUserSettings,
2668+
displayConfig: mockDisplayConfig,
2669+
isSingleSession: () => false,
2670+
isProduction: () => false,
2671+
isDevelopmentOrStaging: () => true,
2672+
organizationSettings: {
2673+
enabled: false,
2674+
},
2675+
}),
2676+
);
2677+
2678+
mockClientFetch.mockReturnValue(
2679+
Promise.resolve({
2680+
signedInSessions: [mockSession],
2681+
}),
2682+
);
2683+
2684+
const sut = new Clerk(developmentPublishableKey);
2685+
2686+
const __internal_openEnableOrganizationsPromptSpy = vi.fn();
2687+
sut.__internal_openEnableOrganizationsPrompt = __internal_openEnableOrganizationsPromptSpy;
2688+
2689+
await sut.load();
2690+
2691+
const result = await sut.__internal_attemptToEnableEnvironmentSetting({
2692+
for: 'organizations',
2693+
caller: 'OrganizationSwitcher',
2694+
});
2695+
2696+
// Contains the organization task, so the prompt should not be opened
2697+
expect(result?.isEnabled).toBe(true);
2698+
expect(__internal_openEnableOrganizationsPromptSpy).not.toHaveBeenCalled();
2699+
});
26332700
});
26342701
});
26352702
});

packages/clerk-js/src/core/clerk.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,11 @@ export class Clerk implements ClerkInterface {
761761

762762
switch (setting) {
763763
case 'organizations': {
764-
const isSettingDisabled = disabledOrganizationsFeature(this, this.environment);
764+
const isSettingDisabled =
765+
disabledOrganizationsFeature(this, this.environment) &&
766+
// Handles case where environment gets enabled via BAPI, but it gets cached and the user is redirected to the choose-organization task
767+
// The enable org prompt should not appear in the task screen since orgs have already been enabled
768+
this.session?.currentTask?.key !== 'choose-organization';
765769

766770
if (!isSettingDisabled) {
767771
return { isEnabled: true };

0 commit comments

Comments
 (0)