Skip to content
Open
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
27 changes: 27 additions & 0 deletions packages/clerk-js/src/core/__tests__/clerk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,33 @@ describe('Clerk singleton', () => {
});
});

it('throws an error when setting active organization with invalid slug', async () => {
const mockSession2 = {
id: '1',
status,
user: {
organizationMemberships: [
{
id: 'orgmem_id',
organization: {
id: 'org_id',
slug: 'valid-org-slug',
},
},
],
},
touch: vi.fn(),
getToken: vi.fn(),
};
mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession2] }));
const sut = new Clerk(productionPublishableKey);
await sut.load();

await expect(sut.setActive({ organization: 'invalid-org-slug' })).rejects.toThrow(
'Unable to find organization with slug "invalid-org-slug". The user is not a member of this organization or it does not exist.',
);
});

it('redirects the user to the /v1/client/touch endpoint if the cookie_expires_at is less than 8 days away', async () => {
mockSession.touch.mockReturnValue(Promise.resolve());
mockClientFetch.mockReturnValue(
Expand Down
10 changes: 10 additions & 0 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,16 @@ export class Clerk implements ClerkInterface {
const newLastActiveOrganizationId = matchingOrganization?.organization.id || null;
const isPersonalWorkspace = newLastActiveOrganizationId === null;

const INVALID_ORGANIZATION_SLUG_ERROR_CODE = 'invalid_organization_slug';

// If a slug was explicitly provided but doesn't match any organization, throw an error
if (organizationIdOrSlug && !matchingOrganization) {
throw new ClerkRuntimeError(
`Unable to find organization with slug "${organizationIdOrSlug}". The user is not a member of this organization or it does not exist.`,
{ code: INVALID_ORGANIZATION_SLUG_ERROR_CODE }
);
}

// Do not update in-memory to personal workspace if force organization selection is enabled
if (this.environment?.organizationSettings?.forceOrganizationSelection && isPersonalWorkspace) {
return;
Expand Down
Loading