Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/afraid-women-buy.md
Original file line number Diff line number Diff line change
@@ -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.
67 changes: 67 additions & 0 deletions packages/clerk-js/src/core/__tests__/clerk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
});
});
});
});
6 changes: 5 additions & 1 deletion packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down