Skip to content

Commit 85a36a8

Browse files
authored
fix(clerk-js): Use afterSwitchSessionUrl switching sessions within UserButton (clerk#4726)
1 parent a6ee05b commit 85a36a8

8 files changed

Lines changed: 35 additions & 34 deletions

File tree

‎.changeset/neat-donkeys-wave.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Bug fix: Use `afterSwitchSessionUrl` instead of using`afterSignInUrl`when switching sessions within`<UserButton/>`.

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,6 @@ describe('Clerk singleton', () => {
564564
expect(mockClientRemoveSessions).toHaveBeenCalled();
565565
expect(sut.setActive).toHaveBeenCalledWith({
566566
session: null,
567-
beforeEmit: expect.any(Function),
568567
redirectUrl: '/',
569568
});
570569
});
@@ -590,7 +589,6 @@ describe('Clerk singleton', () => {
590589
expect(mockSession1.remove).not.toHaveBeenCalled();
591590
expect(sut.setActive).toHaveBeenCalledWith({
592591
session: null,
593-
beforeEmit: expect.any(Function),
594592
redirectUrl: '/',
595593
});
596594
});
@@ -614,7 +612,6 @@ describe('Clerk singleton', () => {
614612
expect(mockClientDestroy).not.toHaveBeenCalled();
615613
expect(sut.setActive).not.toHaveBeenCalledWith({
616614
session: null,
617-
beforeEmit: expect.any(Function),
618615
});
619616
});
620617
});
@@ -637,7 +634,6 @@ describe('Clerk singleton', () => {
637634
expect(mockClientDestroy).not.toHaveBeenCalled();
638635
expect(sut.setActive).toHaveBeenCalledWith({
639636
session: null,
640-
beforeEmit: expect.any(Function),
641637
redirectUrl: '/',
642638
});
643639
});
@@ -653,19 +649,16 @@ describe('Clerk singleton', () => {
653649
);
654650

655651
const sut = new Clerk(productionPublishableKey);
656-
sut.setActive = jest.fn(async ({ beforeEmit }) => void (beforeEmit && beforeEmit()));
657-
sut.navigate = jest.fn();
652+
sut.setActive = jest.fn();
658653
await sut.load();
659654
await sut.signOut({ sessionId: '1', redirectUrl: '/after-sign-out' });
660655
await waitFor(() => {
661656
expect(mockSession1.remove).toHaveBeenCalled();
662657
expect(mockClientDestroy).not.toHaveBeenCalled();
663658
expect(sut.setActive).toHaveBeenCalledWith({
664659
session: null,
665-
beforeEmit: expect.any(Function),
666660
redirectUrl: '/after-sign-out',
667661
});
668-
expect(sut.navigate).toHaveBeenCalledWith('/after-sign-out');
669662
});
670663
});
671664
});

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,21 @@ export class Clerk implements ClerkInterface {
369369
const opts = callbackOrOptions && typeof callbackOrOptions === 'object' ? callbackOrOptions : options || {};
370370

371371
const redirectUrl = opts?.redirectUrl || this.buildAfterSignOutUrl();
372-
const defaultCb = () => this.navigate(redirectUrl);
373-
const cb = typeof callbackOrOptions === 'function' ? callbackOrOptions : defaultCb;
372+
373+
const handleSetActive = () => {
374+
const signOutCallback = typeof callbackOrOptions === 'function' ? callbackOrOptions : undefined;
375+
if (signOutCallback) {
376+
return this.setActive({
377+
session: null,
378+
beforeEmit: ignoreEventValue(signOutCallback),
379+
});
380+
}
381+
382+
return this.setActive({
383+
session: null,
384+
redirectUrl,
385+
});
386+
};
374387

375388
if (!opts.sessionId || this.client.activeSessions.length === 1) {
376389
if (this.#options.experimental?.persistClient ?? true) {
@@ -379,22 +392,14 @@ export class Clerk implements ClerkInterface {
379392
await this.client.destroy();
380393
}
381394

382-
return this.setActive({
383-
session: null,
384-
beforeEmit: ignoreEventValue(cb),
385-
redirectUrl,
386-
});
395+
return handleSetActive();
387396
}
388397

389398
const session = this.client.activeSessions.find(s => s.id === opts.sessionId);
390399
const shouldSignOutCurrent = session?.id && this.session?.id === session.id;
391400
await session?.remove();
392401
if (shouldSignOutCurrent) {
393-
return this.setActive({
394-
session: null,
395-
beforeEmit: ignoreEventValue(cb),
396-
redirectUrl,
397-
});
402+
return handleSetActive();
398403
}
399404
};
400405

@@ -906,12 +911,15 @@ export class Clerk implements ClerkInterface {
906911
if (beforeEmit) {
907912
beforeUnloadTracker?.startTracking();
908913
this.#setTransitiveState();
909-
deprecated('beforeEmit', 'Use the `redirectUrl` property instead.');
914+
deprecated(
915+
'Clerk.setActive({beforeEmit})',
916+
'Use the `redirectUrl` property instead. Example `Clerk.setActive({redirectUrl:"/"})`',
917+
);
910918
await beforeEmit(newSession);
911919
beforeUnloadTracker?.stopTracking();
912920
}
913921

914-
if (redirectUrl) {
922+
if (redirectUrl && !beforeEmit) {
915923
beforeUnloadTracker?.startTracking();
916924
this.#setTransitiveState();
917925

‎packages/clerk-js/src/ui/components/SignIn/SignInAccountSwitcher.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ import { useMultisessionActions } from '../UserButton/useMultisessionActions';
1010
const _SignInAccountSwitcher = () => {
1111
const card = useCardState();
1212
const { userProfileUrl } = useEnvironment().displayConfig;
13-
const { navigateAfterSignIn, afterSignInUrl, path: signInPath } = useSignInContext();
13+
const { afterSignInUrl, path: signInPath } = useSignInContext();
1414
const { navigateAfterSignOut } = useSignOutContext();
1515
const { handleSignOutAllClicked, handleSessionClicked, activeSessions, handleAddAccountClicked } =
1616
useMultisessionActions({
1717
navigateAfterSignOut,
18-
navigateAfterSwitchSession: navigateAfterSignIn,
19-
afterSignInUrl,
18+
afterSwitchSessionUrl: afterSignInUrl,
2019
userProfileUrl,
2120
signInUrl: signInPath,
2221
user: undefined,

‎packages/clerk-js/src/ui/components/UserButton/UserButtonPopover.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
1515
const { close: unsafeClose, ...rest } = props;
1616
const close = () => unsafeClose?.(false);
1717
const { session } = useSession() as { session: ActiveSessionResource };
18-
const { __experimental_asStandalone } = useUserButtonContext();
18+
const userButtonContext = useUserButtonContext();
19+
const { __experimental_asStandalone } = userButtonContext;
1920
const { authConfig } = useEnvironment();
2021
const { user } = useUser();
2122
const {
@@ -26,7 +27,7 @@ export const UserButtonPopover = React.forwardRef<HTMLDivElement, UserButtonPopo
2627
handleSignOutSessionClicked,
2728
handleUserProfileActionClicked,
2829
otherSessions,
29-
} = useMultisessionActions({ ...useUserButtonContext(), actionCompleteCallback: close, user });
30+
} = useMultisessionActions({ ...userButtonContext, actionCompleteCallback: close, user });
3031

3132
return (
3233
<RootBox elementDescriptor={descriptors.userButtonPopoverRootBox}>

‎packages/clerk-js/src/ui/components/UserButton/useMultisessionActions.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ type UseMultisessionActionsParams = {
1212
actionCompleteCallback?: () => void;
1313
navigateAfterSignOut?: () => any;
1414
navigateAfterMultiSessionSingleSignOut?: () => any;
15-
navigateAfterSwitchSession?: () => any;
16-
afterSignInUrl?: string;
15+
afterSwitchSessionUrl?: string;
1716
userProfileUrl?: string;
1817
signInUrl?: string;
1918
} & Pick<UserButtonProps, 'userProfileMode' | 'appearance' | 'userProfileProps'>;
@@ -69,7 +68,7 @@ export const useMultisessionActions = (opts: UseMultisessionActionsParams) => {
6968

7069
const handleSessionClicked = (session: ActiveSessionResource) => async () => {
7170
card.setLoading();
72-
return setActive({ session, redirectUrl: opts.afterSignInUrl }).finally(() => {
71+
return setActive({ session, redirectUrl: opts.afterSwitchSessionUrl }).finally(() => {
7372
card.setIdle();
7473
opts.actionCompleteCallback?.();
7574
});

‎packages/clerk-js/src/ui/contexts/components/SignOut.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ export type SignOutContextType = {
1212
export const useSignOutContext = (): SignOutContextType => {
1313
const { navigate } = useRouter();
1414
const clerk = useClerk();
15-
1615
const navigateAfterSignOut = () => navigate(clerk.buildAfterSignOutUrl());
1716
const navigateAfterMultiSessionSingleSignOutUrl = () => navigate(clerk.buildAfterMultiSessionSingleSignOutUrl());
18-
1917
return {
2018
navigateAfterSignOut,
2119
navigateAfterMultiSessionSingleSignOutUrl,

‎packages/clerk-js/src/ui/contexts/components/UserButton.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export const useUserButtonContext = () => {
4444
const navigateAfterMultiSessionSingleSignOut = () => clerk.redirectWithAuth(afterMultiSessionSingleSignOutUrl);
4545

4646
const afterSwitchSessionUrl = ctx.afterSwitchSessionUrl || displayConfig.afterSwitchSessionUrl;
47-
const navigateAfterSwitchSession = () => navigate(afterSwitchSessionUrl);
4847

4948
const userProfileMode = !!ctx.userProfileUrl && !ctx.userProfileMode ? 'navigation' : ctx.userProfileMode;
5049

@@ -57,7 +56,6 @@ export const useUserButtonContext = () => {
5756
componentName,
5857
navigateAfterMultiSessionSingleSignOut,
5958
navigateAfterSignOut,
60-
navigateAfterSwitchSession,
6159
signInUrl,
6260
userProfileUrl,
6361
afterMultiSessionSingleSignOutUrl,

0 commit comments

Comments
 (0)