Skip to content

Commit 4845bf1

Browse files
cursoragentjescalan
andcommitted
Fix: Throw error when setting active org with invalid slug
Co-authored-by: jeff <[email protected]>
1 parent 9631927 commit 4845bf1

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,33 @@ describe('Clerk singleton', () => {
378378
});
379379
});
380380

381+
it('throws an error when setting active organization with invalid slug', async () => {
382+
const mockSession2 = {
383+
id: '1',
384+
status,
385+
user: {
386+
organizationMemberships: [
387+
{
388+
id: 'orgmem_id',
389+
organization: {
390+
id: 'org_id',
391+
slug: 'valid-org-slug',
392+
},
393+
},
394+
],
395+
},
396+
touch: vi.fn(),
397+
getToken: vi.fn(),
398+
};
399+
mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession2] }));
400+
const sut = new Clerk(productionPublishableKey);
401+
await sut.load();
402+
403+
await expect(sut.setActive({ organization: 'invalid-org-slug' })).rejects.toThrow(
404+
'Unable to find organization with slug "invalid-org-slug". The user is not a member of this organization or it does not exist.',
405+
);
406+
});
407+
381408
it('redirects the user to the /v1/client/touch endpoint if the cookie_expires_at is less than 8 days away', async () => {
382409
mockSession.touch.mockReturnValue(Promise.resolve());
383410
mockClientFetch.mockReturnValue(

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,13 @@ export class Clerk implements ClerkInterface {
13511351
const newLastActiveOrganizationId = matchingOrganization?.organization.id || null;
13521352
const isPersonalWorkspace = newLastActiveOrganizationId === null;
13531353

1354+
// If a slug was explicitly provided but doesn't match any organization, throw an error
1355+
if (organizationIdOrSlug && !matchingOrganization) {
1356+
throw new Error(
1357+
`Unable to find organization with slug "${organizationIdOrSlug}". The user is not a member of this organization or it does not exist.`,
1358+
);
1359+
}
1360+
13541361
// Do not update in-memory to personal workspace if force organization selection is enabled
13551362
if (this.environment?.organizationSettings?.forceOrganizationSelection && isPersonalWorkspace) {
13561363
return;

0 commit comments

Comments
 (0)