Skip to content

Commit 485407e

Browse files
committed
Refactor after-auth flows to keep navigation internally
1 parent fdd4f0b commit 485407e

File tree

7 files changed

+27
-41
lines changed

7 files changed

+27
-41
lines changed

.changeset/smart-socks-refuse.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+
Refactor after-auth flows to keep navigation internally

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,7 +2365,7 @@ describe('Clerk singleton', () => {
23652365
await sut.load(mockedLoadOptions);
23662366

23672367
await sut.setActive({ session: mockResource as any as PendingSessionResource });
2368-
await sut.__experimental_navigateToTask();
2368+
await sut.__internal_navigateToTaskIfAvailable();
23692369

23702370
expect(mockNavigate.mock.calls[0][0]).toBe('/sign-in#/tasks/add-organization');
23712371
});
@@ -2409,7 +2409,7 @@ describe('Clerk singleton', () => {
24092409
await sut.setActive({ session: mockSession as any as ActiveSessionResource });
24102410

24112411
const redirectUrlComplete = '/welcome-to-app';
2412-
await sut.__experimental_navigateToTask({ redirectUrlComplete });
2412+
await sut.__internal_navigateToTaskIfAvailable({ redirectUrlComplete });
24132413

24142414
console.log(mockNavigate.mock.calls);
24152415

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

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import type {
2929
AuthenticateWithGoogleOneTapParams,
3030
AuthenticateWithMetamaskParams,
3131
AuthenticateWithOKXWalletParams,
32-
Clerk as ClerkInterface,
3332
ClerkAPIError,
3433
ClerkAuthenticateWithWeb3Params,
34+
Clerk as ClerkInterface,
3535
ClerkOptions,
3636
ClientJSONSnapshot,
3737
ClientResource,
@@ -1317,9 +1317,8 @@ export class Clerk implements ClerkInterface {
13171317
eventBus.emit(events.TokenUpdate, { token: null });
13181318
}
13191319

1320-
// Only triggers navigation for internal AIO components routing or multi-session switch
1321-
const isSwitchingSessions = this.session?.id != session.id;
1322-
const shouldNavigateOnSetActive = this.#componentNavigationContext || isSwitchingSessions;
1320+
// Only triggers navigation for internal AIO components routing
1321+
const shouldNavigateOnSetActive = this.#componentNavigationContext;
13231322
if (newSession?.currentTask && shouldNavigateOnSetActive) {
13241323
await navigateToTask(session.currentTask.key, {
13251324
options: this.#options,
@@ -1333,16 +1332,7 @@ export class Clerk implements ClerkInterface {
13331332
this.#emit();
13341333
};
13351334

1336-
public __experimental_navigateToTask = async ({ redirectUrlComplete }: NextTaskParams = {}): Promise<void> => {
1337-
/**
1338-
* Invalidate previously cached pages with auth state before navigating
1339-
*/
1340-
const onBeforeSetActive: SetActiveHook =
1341-
typeof window !== 'undefined' && typeof window.__unstable__onBeforeSetActive === 'function'
1342-
? window.__unstable__onBeforeSetActive
1343-
: noop;
1344-
await onBeforeSetActive();
1345-
1335+
public __internal_navigateToTaskIfAvailable = async ({ redirectUrlComplete }: NextTaskParams = {}): Promise<void> => {
13461336
const session = this.session;
13471337
if (!session || !this.environment) {
13481338
return;
@@ -1368,20 +1358,6 @@ export class Clerk implements ClerkInterface {
13681358
if (tracker.isUnloading()) {
13691359
return;
13701360
}
1371-
1372-
this.#setAccessors(session);
1373-
this.#emit();
1374-
1375-
/**
1376-
* Invoke the Next.js middleware to synchronize server and client state after resolving a session task.
1377-
* This ensures that any server-side logic depending on the session status (like middleware-based
1378-
* redirects or protected routes) correctly reflects the updated client authentication state.
1379-
*/
1380-
const onAfterSetActive: SetActiveHook =
1381-
typeof window !== 'undefined' && typeof window.__unstable__onAfterSetActive === 'function'
1382-
? window.__unstable__onAfterSetActive
1383-
: noop;
1384-
await onAfterSetActive();
13851361
};
13861362

13871363
public addListener = (listener: ListenerCallback): UnsubscribeCallback => {

packages/clerk-js/src/ui/components/SessionTasks/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const SessionTasksStart = () => {
2020
useEffect(() => {
2121
// Simulates additional latency to avoid a abrupt UI transition when navigating to the next task
2222
const timeoutId = setTimeout(() => {
23-
void clerk.__experimental_navigateToTask({ redirectUrlComplete });
23+
void clerk.__internal_navigateToTaskIfAvailable({ redirectUrlComplete });
2424
}, 500);
2525
return () => clearTimeout(timeoutId);
2626
}, [navigate, clerk, redirectUrlComplete]);
@@ -84,7 +84,9 @@ export const SessionTask = withCardStateProvider(() => {
8484

8585
const nextTask = useCallback(() => {
8686
setIsNavigatingToTask(true);
87-
return clerk.__experimental_navigateToTask({ redirectUrlComplete }).finally(() => setIsNavigatingToTask(false));
87+
return clerk
88+
.__internal_navigateToTaskIfAvailable({ redirectUrlComplete })
89+
.finally(() => setIsNavigatingToTask(false));
8890
}, [clerk, redirectUrlComplete]);
8991

9092
if (!clerk.session?.currentTask) {

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type UseMultisessionActionsParams = {
1919
} & Pick<UserButtonProps, 'userProfileMode' | 'appearance' | 'userProfileProps'>;
2020

2121
export const useMultisessionActions = (opts: UseMultisessionActionsParams) => {
22-
const { setActive, signOut, openUserProfile } = useClerk();
22+
const { setActive, signOut, openUserProfile, __internal_navigateToTaskIfAvailable } = useClerk();
2323
const card = useCardState();
2424
const { signedInSessions, otherSessions } = useMultipleSessions({ user: opts.user });
2525
const { navigate } = useRouter();
@@ -69,10 +69,13 @@ export const useMultisessionActions = (opts: UseMultisessionActionsParams) => {
6969

7070
const handleSessionClicked = (session: SignedInSessionResource) => async () => {
7171
card.setLoading();
72-
return setActive({ session, redirectUrl: opts.afterSwitchSessionUrl }).finally(() => {
73-
card.setIdle();
74-
opts.actionCompleteCallback?.();
75-
});
72+
73+
return setActive({ session, redirectUrl: opts.afterSwitchSessionUrl })
74+
.then(() => __internal_navigateToTaskIfAvailable())
75+
.finally(() => {
76+
card.setIdle();
77+
opts.actionCompleteCallback?.();
78+
});
7679
};
7780

7881
const handleAddAccountClicked = () => {

packages/react/src/isomorphicClerk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,9 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
731731
}
732732
};
733733

734-
__experimental_navigateToTask = async (params?: NextTaskParams): Promise<void> => {
734+
__internal_navigateToTaskIfAvailable = async (params?: NextTaskParams): Promise<void> => {
735735
if (this.clerkjs) {
736-
return this.clerkjs.__experimental_navigateToTask(params);
736+
return this.clerkjs.__internal_navigateToTaskIfAvailable(params);
737737
} else {
738738
return Promise.reject();
739739
}

packages/types/src/clerk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,9 @@ export interface Clerk {
818818
* Navigates to the next task or redirects to completion URL.
819819
* If the current session has pending tasks, it navigates to the next task.
820820
* If all tasks are complete, it navigates to the provided completion URL or defaults to the origin redirect URL (either from sign-in or sign-up).
821-
* @experimental
821+
* @internal
822822
*/
823-
__experimental_navigateToTask: (params?: NextTaskParams) => Promise<void>;
823+
__internal_navigateToTaskIfAvailable: (params?: NextTaskParams) => Promise<void>;
824824

825825
/**
826826
* This is an optional function.

0 commit comments

Comments
 (0)