Skip to content

Commit 33446b8

Browse files
committed
Replace onPendingSession with navigate
1 parent 3a316b7 commit 33446b8

18 files changed

+134
-100
lines changed

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,18 @@ describe('Clerk singleton', () => {
444444
expect(sut.navigate).toHaveBeenCalledWith('/redirect-url-path');
445445
});
446446

447+
it('calls `navigate`', async () => {
448+
mockSession.touch.mockReturnValue(Promise.resolve());
449+
mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession] }));
450+
const navigate = jest.fn();
451+
452+
const sut = new Clerk(productionPublishableKey);
453+
await sut.load();
454+
await sut.setActive({ session: mockSession as any as PendingSessionResource, navigate });
455+
expect(mockSession.touch).toHaveBeenCalled();
456+
expect(navigate).toHaveBeenCalled();
457+
});
458+
447459
mockNativeRuntime(() => {
448460
it('calls session.touch in a non-standard browser', async () => {
449461
mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession] }));
@@ -484,7 +496,7 @@ describe('Clerk singleton', () => {
484496
getToken: jest.fn(),
485497
lastActiveToken: { getRawString: () => 'mocked-token' },
486498
tasks: [{ key: 'choose-organization' }],
487-
currentTask: { key: 'choose-organization', __internal_getUrl: () => 'https://sut/tasks/choose-organization' },
499+
currentTask: { key: 'choose-organization' },
488500
reload: jest.fn(() =>
489501
Promise.resolve({
490502
id: '1',
@@ -493,7 +505,6 @@ describe('Clerk singleton', () => {
493505
tasks: [{ key: 'choose-organization' }],
494506
currentTask: {
495507
key: 'choose-organization',
496-
__internal_getUrl: () => 'https://sut/tasks/choose-organization',
497508
},
498509
}),
499510
),
@@ -524,46 +535,32 @@ describe('Clerk singleton', () => {
524535
expect(mockSession.touch).toHaveBeenCalled();
525536
});
526537

527-
it('calls `onPendingSession` from clerk options', async () => {
538+
it('navigate to `taskUrl` option', async () => {
528539
mockSession.touch.mockReturnValue(Promise.resolve());
529540
mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession] }));
530-
const onPendingSession = jest.fn();
531541

532542
const sut = new Clerk(productionPublishableKey);
543+
sut.navigate = jest.fn();
533544
await sut.load({
534-
onPendingSession,
545+
taskUrls: {
546+
'choose-organization': '/choose-organization',
547+
},
535548
});
536549
await sut.setActive({ session: mockSession as any as PendingSessionResource });
537550
expect(mockSession.touch).toHaveBeenCalled();
538-
expect(onPendingSession).toHaveBeenCalled();
551+
expect(sut.navigate).toHaveBeenCalledWith('/choose-organization');
539552
});
540553

541-
it('calls `onPendingSession` as argument', async () => {
554+
it('calls `navigate`', async () => {
542555
mockSession.touch.mockReturnValue(Promise.resolve());
543556
mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession] }));
544-
const onPendingSession = jest.fn();
557+
const navigate = jest.fn();
545558

546559
const sut = new Clerk(productionPublishableKey);
547560
await sut.load();
548-
await sut.setActive({ session: mockSession as any as PendingSessionResource, onPendingSession });
549-
expect(mockSession.touch).toHaveBeenCalled();
550-
expect(onPendingSession).toHaveBeenCalled();
551-
});
552-
553-
it('if `onPendingSession` is not defined, navigate to `taskUrl` option', async () => {
554-
mockSession.touch.mockReturnValue(Promise.resolve());
555-
mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession] }));
556-
557-
const sut = new Clerk(productionPublishableKey);
558-
sut.navigate = jest.fn();
559-
await sut.load({
560-
taskUrls: {
561-
'choose-organization': '/choose-organization',
562-
},
563-
});
564-
await sut.setActive({ session: mockSession as any as PendingSessionResource });
561+
await sut.setActive({ session: mockSession as any as PendingSessionResource, navigate });
565562
expect(mockSession.touch).toHaveBeenCalled();
566-
expect(sut.navigate).toHaveBeenCalledWith('/choose-organization');
563+
expect(navigate).toHaveBeenCalled();
567564
});
568565
});
569566

@@ -972,7 +969,7 @@ describe('Clerk singleton', () => {
972969
status: 'pending',
973970
user: {},
974971
tasks: [{ key: 'choose-organization' }],
975-
currentTask: { key: 'choose-organization', __internal_getUrl: () => 'https://sut/tasks/choose-organization' },
972+
currentTask: { key: 'choose-organization' },
976973
lastActiveToken: { getRawString: () => 'mocked-token' },
977974
};
978975

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import type {
2828
AuthenticateWithGoogleOneTapParams,
2929
AuthenticateWithMetamaskParams,
3030
AuthenticateWithOKXWalletParams,
31+
Clerk as ClerkInterface,
3132
ClerkAPIError,
3233
ClerkAuthenticateWithWeb3Params,
33-
Clerk as ClerkInterface,
3434
ClerkOptions,
3535
ClientJSONSnapshot,
3636
ClientResource,
@@ -49,7 +49,6 @@ import type {
4949
JoinWaitlistParams,
5050
ListenerCallback,
5151
NavigateOptions,
52-
OnPendingSessionFn,
5352
OrganizationListProps,
5453
OrganizationProfileProps,
5554
OrganizationResource,
@@ -62,6 +61,7 @@ import type {
6261
RedirectOptions,
6362
Resources,
6463
SDKMetadata,
64+
SetActiveNavigate,
6565
SetActiveParams,
6666
SignedInSessionResource,
6767
SignInProps,
@@ -1300,27 +1300,27 @@ export class Clerk implements ClerkInterface {
13001300
});
13011301
}
13021302

1303-
const shouldNavigate = (redirectUrl || setActiveNavigate) && !beforeEmit;
1303+
const taskUrl =
1304+
sessionIsPending && this.session?.currentTask && this.#options.taskUrls?.[this.session?.currentTask.key];
1305+
const shouldNavigate = (redirectUrl || taskUrl || setActiveNavigate) && !beforeEmit;
13041306
if (shouldNavigate) {
13051307
await tracker.track(async () => {
13061308
if (!this.client) {
13071309
// Typescript is not happy because since thinks this.client might have changed to undefined because the function is asynchronous.
13081310
return;
13091311
}
13101312

1311-
if (sessionIsPending) {
1313+
if (!sessionIsPending) {
13121314
this.#setTransitiveState();
13131315
}
13141316

1315-
if (setActiveNavigate) {
1316-
await setActiveNavigate();
1317+
if (taskUrl) {
1318+
await this.navigate(taskUrl);
13171319
return;
13181320
}
13191321

1320-
const taskUrl =
1321-
sessionIsPending && this.session?.currentTask && this.#options.taskUrls?.[this.session?.currentTask.key];
1322-
if (taskUrl) {
1323-
await this.navigate(taskUrl);
1322+
if (setActiveNavigate && newSession) {
1323+
await setActiveNavigate({ session: newSession });
13241324
return;
13251325
}
13261326

@@ -1821,18 +1821,18 @@ export class Clerk implements ClerkInterface {
18211821
});
18221822
};
18231823

1824-
const onPendingSession: OnPendingSessionFn = ({ session }) =>
1825-
navigateToTask(session, {
1824+
const setActiveNavigate: SetActiveNavigate = async ({ session }) => {
1825+
await navigateToTask(session, {
18261826
baseUrl: displayConfig.signInUrl,
18271827
navigate: this.navigate,
1828-
options: this.#options,
18291828
});
1829+
};
18301830

18311831
if (si.status === 'complete') {
18321832
return this.setActive({
18331833
session: si.sessionId,
18341834
redirectUrl: redirectUrls.getAfterSignInUrl(),
1835-
onPendingSession,
1835+
navigate: setActiveNavigate,
18361836
});
18371837
}
18381838

@@ -1846,7 +1846,7 @@ export class Clerk implements ClerkInterface {
18461846
return this.setActive({
18471847
session: res.createdSessionId,
18481848
redirectUrl: redirectUrls.getAfterSignInUrl(),
1849-
onPendingSession,
1849+
navigate: setActiveNavigate,
18501850
});
18511851
case 'needs_first_factor':
18521852
return navigateToFactorOne();
@@ -1896,7 +1896,7 @@ export class Clerk implements ClerkInterface {
18961896
return this.setActive({
18971897
session: res.createdSessionId,
18981898
redirectUrl: redirectUrls.getAfterSignUpUrl(),
1899-
onPendingSession,
1899+
navigate: setActiveNavigate,
19001900
});
19011901
case 'missing_requirements':
19021902
return navigateToNextStepSignUp({ missingFields: res.missingFields });
@@ -1909,7 +1909,7 @@ export class Clerk implements ClerkInterface {
19091909
return this.setActive({
19101910
session: su.sessionId,
19111911
redirectUrl: redirectUrls.getAfterSignUpUrl(),
1912-
onPendingSession,
1912+
navigate: setActiveNavigate,
19131913
});
19141914
}
19151915

@@ -1934,7 +1934,7 @@ export class Clerk implements ClerkInterface {
19341934
return this.setActive({
19351935
session: sessionId,
19361936
redirectUrl: redirectUrls.getAfterSignInUrl(),
1937-
onPendingSession,
1937+
navigate: setActiveNavigate,
19381938
});
19391939
}
19401940
}
@@ -2124,12 +2124,12 @@ export class Clerk implements ClerkInterface {
21242124
await this.setActive({
21252125
session: signInOrSignUp.createdSessionId,
21262126
redirectUrl,
2127-
onPendingSession: ({ session }) =>
2128-
navigateToTask(session, {
2127+
navigate: async ({ session }) => {
2128+
await navigateToTask(session, {
21292129
baseUrl: displayConfig.signInUrl,
21302130
navigate: this.navigate,
2131-
options: this.#options,
2132-
}),
2131+
});
2132+
},
21332133
});
21342134
}
21352135
break;
@@ -2346,8 +2346,8 @@ export class Clerk implements ClerkInterface {
23462346
this.#authService = await AuthCookieService.create(
23472347
this,
23482348
this.#fapiClient,
2349-
2350-
this.#instanceType,
2349+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2350+
this.#instanceType!,
23512351
this.#publicEventBus,
23522352
);
23532353

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ClerkOptions, PendingSessionResource, SessionTask } from '@clerk/types';
1+
import type { SessionResource, SessionTask } from '@clerk/types';
22

33
import { buildURL } from '../utils';
44

@@ -26,18 +26,19 @@ export function buildTaskURL(task: SessionTask, opts: Pick<Parameters<typeof bui
2626
* @internal
2727
*/
2828
export function navigateToTask(
29-
session: PendingSessionResource,
29+
session: SessionResource,
3030
{
31-
options,
3231
navigate,
3332
baseUrl,
3433
}: {
3534
navigate: (to: string) => Promise<unknown>;
3635
baseUrl: string;
37-
options?: ClerkOptions;
3836
},
3937
) {
40-
const currentTaskKey = session.currentTask.key;
38+
const currentTask = session.currentTask;
39+
if (!currentTask) {
40+
return;
41+
}
4142

42-
return navigate(options?.taskUrls?.[currentTaskKey] ?? buildTaskURL(session.currentTask, { base: baseUrl }));
43+
return navigate(buildTaskURL(currentTask, { base: baseUrl }));
4344
}

packages/clerk-js/src/ui/components/SignIn/SignInFactorOneAlternativeChannelCodeForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const SignInFactorOneAlternativeChannelCodeForm = (props: SignInFactorOne
6868
return setActive({
6969
session: res.createdSessionId,
7070
redirectUrl: afterSignInUrl,
71-
onPendingSession,
71+
navigate: onPendingSession,
7272
});
7373
case 'needs_second_factor':
7474
return navigate('../factor-two');

packages/clerk-js/src/ui/components/SignIn/SignInFactorOneCodeForm.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ export const SignInFactorOneCodeForm = (props: SignInFactorOneCodeFormProps) =>
104104

105105
switch (res.status) {
106106
case 'complete':
107-
return setActive({ session: res.createdSessionId, redirectUrl: afterSignInUrl, onPendingSession });
107+
return setActive({
108+
session: res.createdSessionId,
109+
redirectUrl: afterSignInUrl,
110+
navigate: onPendingSession,
111+
});
108112
case 'needs_second_factor':
109113
return navigate('../factor-two');
110114
case 'needs_new_password':

packages/clerk-js/src/ui/components/SignIn/SignInFactorOnePasswordCard.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ export const SignInFactorOnePasswordCard = (props: SignInFactorOnePasswordProps)
7474
.then(res => {
7575
switch (res.status) {
7676
case 'complete':
77-
return setActive({ session: res.createdSessionId, redirectUrl: afterSignInUrl, onPendingSession });
77+
return setActive({
78+
session: res.createdSessionId,
79+
redirectUrl: afterSignInUrl,
80+
navigate: onPendingSession,
81+
});
7882
case 'needs_second_factor':
7983
return navigate('../factor-two');
8084
default:

packages/clerk-js/src/ui/components/SignIn/SignInFactorTwoBackupCodeCard.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ export const SignInFactorTwoBackupCodeCard = (props: SignInFactorTwoBackupCodeCa
5252
queryParams.set('createdSessionId', res.createdSessionId);
5353
return navigate(`../reset-password-success?${queryParams.toString()}`);
5454
}
55-
return setActive({ session: res.createdSessionId, redirectUrl: afterSignInUrl, onPendingSession });
55+
return setActive({
56+
session: res.createdSessionId,
57+
redirectUrl: afterSignInUrl,
58+
navigate: onPendingSession,
59+
});
5660
default:
5761
return console.error(clerkInvalidFAPIResponse(res.status, supportEmail));
5862
}

packages/clerk-js/src/ui/components/SignIn/SignInFactorTwoCodeForm.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ export const SignInFactorTwoCodeForm = (props: SignInFactorTwoCodeFormProps) =>
7979
queryParams.set('createdSessionId', res.createdSessionId);
8080
return navigate(`../reset-password-success?${queryParams.toString()}`);
8181
}
82-
return setActive({ session: res.createdSessionId, redirectUrl: afterSignInUrl, onPendingSession });
82+
return setActive({
83+
session: res.createdSessionId,
84+
redirectUrl: afterSignInUrl,
85+
navigate: onPendingSession,
86+
});
8387
default:
8488
return console.error(clerkInvalidFAPIResponse(res.status, supportEmail));
8589
}

packages/clerk-js/src/ui/components/SignIn/SignInStart.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ function SignInStartInternal(): JSX.Element {
237237
return clerk.setActive({
238238
session: res.createdSessionId,
239239
redirectUrl: afterSignInUrl,
240-
onPendingSession,
240+
navigate: onPendingSession,
241241
});
242242
default: {
243243
console.error(clerkInvalidFAPIResponse(res.status, supportEmail));
@@ -389,7 +389,7 @@ function SignInStartInternal(): JSX.Element {
389389
return clerk.setActive({
390390
session: res.createdSessionId,
391391
redirectUrl: afterSignInUrl,
392-
onPendingSession,
392+
navigate: onPendingSession,
393393
});
394394
default: {
395395
console.error(clerkInvalidFAPIResponse(res.status, supportEmail));

packages/clerk-js/src/ui/components/SignIn/shared.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ function useHandleAuthenticateWithPasskey(onSecondFactor: () => Promise<unknown>
3535
return setActive({
3636
session: res.createdSessionId,
3737
redirectUrl: afterSignInUrl,
38-
onPendingSession: ({ session }) =>
39-
navigateToTask(session, {
38+
navigate: async ({ session }) => {
39+
await navigateToTask(session, {
4040
baseUrl: signInUrl,
4141
navigate: navigate,
42-
}),
42+
});
43+
},
4344
});
4445
case 'needs_second_factor':
4546
return onSecondFactor();

0 commit comments

Comments
 (0)