Skip to content

Commit

Permalink
fix(clerk-js): Navigate to /choose when signing out during multi se…
Browse files Browse the repository at this point in the history
…ssion (#4203)
  • Loading branch information
alexcarpenter authored Sep 30, 2024
1 parent e483037 commit a361200
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-nails-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/clerk-js": patch
---

Navigate to `/choose` when signing out during multi session.
11 changes: 10 additions & 1 deletion packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,16 @@ export class Clerk implements ClerkInterface {

public buildAfterMultiSessionSingleSignOutUrl(): string {
if (!this.#options.afterMultiSessionSingleSignOutUrl) {
return this.buildAfterSignOutUrl();
return this.buildUrlWithAuth(
buildURL(
{
base: this.#options.signInUrl
? `${this.#options.signInUrl}/choose`
: this.environment?.displayConfig.afterSignOutOneUrl,
},
{ stringify: true },
),
);
}

return this.buildUrlWithAuth(this.#options.afterMultiSessionSingleSignOutUrl);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe } from '@jest/globals';

import { render } from '../../../../testUtils';
import { render, waitFor } from '../../../../testUtils';
import { bindCreateFixtures } from '../../../utils/test/createFixtures';
import { UserButton } from '../UserButton';

Expand Down Expand Up @@ -139,11 +139,30 @@ describe('UserButton', () => {

it('signs out of the currently active session when clicking "Sign out"', async () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.signOut.mockReturnValueOnce(Promise.resolve());
fixtures.clerk.signOut.mockImplementationOnce(callback => {
return Promise.resolve(callback());
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out'));
expect(fixtures.clerk.signOut).toHaveBeenCalledWith(expect.any(Function), { sessionId: '0' });
await waitFor(() => {
expect(fixtures.clerk.signOut).toHaveBeenCalledWith(expect.any(Function), { sessionId: '0' });
expect(fixtures.clerk.redirectWithAuth).toHaveBeenCalledWith('https://accounts.clerk.com/sign-in/choose');
});
});

it('signs out of all currently active session when clicking "Sign out of all accounts"', async () => {
const { wrapper, fixtures } = await createFixtures(initConfig);
fixtures.clerk.signOut.mockImplementationOnce(callback => {
return Promise.resolve(callback());
});
const { getByText, getByRole, userEvent } = render(<UserButton />, { wrapper });
await userEvent.click(getByRole('button', { name: 'Open user button' }));
await userEvent.click(getByText('Sign out of all accounts'));
await waitFor(() => {
expect(fixtures.clerk.signOut).toHaveBeenCalledWith(expect.any(Function));
expect(fixtures.router.navigate).toHaveBeenCalledWith('/');
});
});
});

Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/ui/utils/test/mockHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const mockClerkMethods = (clerk: LoadedClerk): DeepJestMocked<LoadedClerk
});
mockProp(clerk, 'navigate');
mockProp(clerk, 'setActive');
mockProp(clerk, 'redirectWithAuth');
mockProp(clerk, '__internal_navigateWithError');
return clerk as any as DeepJestMocked<LoadedClerk>;
};
Expand Down

0 comments on commit a361200

Please sign in to comment.