Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(clerk-js): Use afterSwitchSessionUrl switching sessions within UserButton #4726

5 changes: 5 additions & 0 deletions .changeset/neat-donkeys-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Bug fix: Use `afterSwitchSessionUrl` instead of using`afterSignInUrl`when switching sessions within`<UserButton/>`.
2 changes: 1 addition & 1 deletion packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ export class Clerk implements ClerkInterface {
beforeUnloadTracker?.stopTracking();
}

if (redirectUrl) {
if (redirectUrl && !beforeEmit) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clerk.signOut() uses both beforeEmit and redirectUrl which is an incorrect state as the redirection should be trigger by only one of them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we fix signOut? Then throw in this if both beforeEmit and redirectUrl come through.

beforeUnloadTracker?.startTracking();
this.#setTransitiveState();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import { useMultisessionActions } from '../UserButton/useMultisessionActions';
const _SignInAccountSwitcher = () => {
const card = useCardState();
const { userProfileUrl } = useEnvironment().displayConfig;
const { navigateAfterSignIn, afterSignInUrl, path: signInPath } = useSignInContext();
const { afterSignInUrl, path: signInPath } = useSignInContext();
const { navigateAfterSignOut } = useSignOutContext();
const { handleSignOutAllClicked, handleSessionClicked, activeSessions, handleAddAccountClicked } =
useMultisessionActions({
navigateAfterSignOut,
navigateAfterSwitchSession: navigateAfterSignIn,
afterSignInUrl,
afterSwitchSession: afterSignInUrl,
userProfileUrl,
signInUrl: signInPath,
user: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ type UseMultisessionActionsParams = {
actionCompleteCallback?: () => void;
navigateAfterSignOut?: () => any;
navigateAfterMultiSessionSingleSignOut?: () => any;
Ephem marked this conversation as resolved.
Show resolved Hide resolved
navigateAfterSwitchSession?: () => any;
afterSignInUrl?: string;
afterSwitchSession?: string;
panteliselef marked this conversation as resolved.
Show resolved Hide resolved
userProfileUrl?: string;
signInUrl?: string;
} & Pick<UserButtonProps, 'userProfileMode' | 'appearance' | 'userProfileProps'>;
Expand Down Expand Up @@ -69,7 +68,7 @@ export const useMultisessionActions = (opts: UseMultisessionActionsParams) => {

const handleSessionClicked = (session: ActiveSessionResource) => async () => {
card.setLoading();
return setActive({ session, redirectUrl: opts.afterSignInUrl }).finally(() => {
return setActive({ session, redirectUrl: opts.afterSwitchSession }).finally(() => {
panteliselef marked this conversation as resolved.
Show resolved Hide resolved
card.setIdle();
opts.actionCompleteCallback?.();
});
Expand Down
2 changes: 0 additions & 2 deletions packages/clerk-js/src/ui/contexts/components/UserButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export const useUserButtonContext = () => {
const navigateAfterMultiSessionSingleSignOut = () => clerk.redirectWithAuth(afterMultiSessionSingleSignOutUrl);

const afterSwitchSessionUrl = ctx.afterSwitchSessionUrl || displayConfig.afterSwitchSessionUrl;
const navigateAfterSwitchSession = () => navigate(afterSwitchSessionUrl);

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

Expand All @@ -57,7 +56,6 @@ export const useUserButtonContext = () => {
componentName,
navigateAfterMultiSessionSingleSignOut,
navigateAfterSignOut,
navigateAfterSwitchSession,
signInUrl,
userProfileUrl,
afterMultiSessionSingleSignOutUrl,
Expand Down
Loading