Skip to content

Commit 3a316b7

Browse files
Remove #handlePendingSession branch and introduce navigate option
Co-authored-by: Laura Beatris <[email protected]>
1 parent 0c857cb commit 3a316b7

File tree

2 files changed

+57
-86
lines changed

2 files changed

+57
-86
lines changed

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

Lines changed: 55 additions & 79 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,
3231
ClerkAPIError,
3332
ClerkAuthenticateWithWeb3Params,
33+
Clerk as ClerkInterface,
3434
ClerkOptions,
3535
ClientJSONSnapshot,
3636
ClientResource,
@@ -54,7 +54,6 @@ import type {
5454
OrganizationProfileProps,
5555
OrganizationResource,
5656
OrganizationSwitcherProps,
57-
PendingSessionResource,
5857
PricingTableProps,
5958
PublicKeyCredentialCreationOptionsWithoutExtensions,
6059
PublicKeyCredentialRequestOptionsWithoutExtensions,
@@ -119,7 +118,7 @@ import {
119118
stripOrigin,
120119
windowNavigate,
121120
} from '../utils';
122-
import { assertNoLegacyProp, warnNoTaskOptions } from '../utils/assertNoLegacyProp';
121+
import { assertNoLegacyProp } from '../utils/assertNoLegacyProp';
123122
import { CLERK_ENVIRONMENT_STORAGE_ENTRY, SafeLocalStorage } from '../utils/localStorage';
124123
import { memoizeListenerCallback } from '../utils/memoizeStateListenerCallback';
125124
import { RedirectUrls } from '../utils/redirectUrls';
@@ -1199,14 +1198,11 @@ export class Clerk implements ClerkInterface {
11991198
/**
12001199
* `setActive` can be used to set the active session and/or organization.
12011200
*/
1202-
public setActive = async ({
1203-
session,
1204-
organization,
1205-
beforeEmit,
1206-
redirectUrl,
1207-
onPendingSession,
1208-
}: SetActiveParams): Promise<void> => {
1201+
public setActive = async (params: SetActiveParams): Promise<void> => {
1202+
const { organization, beforeEmit, redirectUrl, navigate: setActiveNavigate } = params;
1203+
let { session } = params;
12091204
this.__internal_setActiveInProgress = true;
1205+
12101206
try {
12111207
if (!this.client) {
12121208
throw new Error('setActive is being called before the client is loaded. Wait for init.');
@@ -1218,6 +1214,10 @@ export class Clerk implements ClerkInterface {
12181214
);
12191215
}
12201216

1217+
if (typeof session === 'string') {
1218+
session = (this.client.sessions.find(x => x.id === session) as SignedInSessionResource) || null;
1219+
}
1220+
12211221
const onBeforeSetActive: SetActiveHook =
12221222
typeof window !== 'undefined' && typeof window.__unstable__onBeforeSetActive === 'function'
12231223
? window.__unstable__onBeforeSetActive
@@ -1228,11 +1228,8 @@ export class Clerk implements ClerkInterface {
12281228
? window.__unstable__onAfterSetActive
12291229
: noop;
12301230

1231-
if (typeof session === 'string') {
1232-
session = (this.client.sessions.find(x => x.id === session) as SignedInSessionResource) || null;
1233-
}
1234-
12351231
let newSession = session === undefined ? this.session : session;
1232+
const sessionIsPending = newSession?.status === 'pending';
12361233

12371234
// At this point, the `session` variable should contain either an `SignedInSessionResource`
12381235
// ,`null` or `undefined`.
@@ -1263,20 +1260,14 @@ export class Clerk implements ClerkInterface {
12631260
}
12641261
}
12651262

1266-
if (newSession?.status === 'pending') {
1267-
warnNoTaskOptions({
1268-
...this.#options,
1269-
onPendingSession: this.#options['onPendingSession'] ?? onPendingSession,
1270-
});
1271-
await this.#handlePendingSession(newSession, onPendingSession);
1272-
return;
1263+
// Do not revalidate server cache for pending sessions to avoid unmount of `SignIn/SignUp` AIOs when navigating to task
1264+
if (!sessionIsPending) {
1265+
/**
1266+
* Hint to each framework, that the user will be signed out when `{session: null}` is provided.
1267+
*/
1268+
await onBeforeSetActive(newSession === null ? 'sign-out' : undefined);
12731269
}
12741270

1275-
/**
1276-
* Hint to each framework, that the user will be signed out when `{session: null}` is provided.
1277-
*/
1278-
await onBeforeSetActive(newSession === null ? 'sign-out' : undefined);
1279-
12801271
//1. setLastActiveSession to passed user session (add a param).
12811272
// Note that this will also update the session's active organization
12821273
// id.
@@ -1309,19 +1300,44 @@ export class Clerk implements ClerkInterface {
13091300
});
13101301
}
13111302

1312-
if (redirectUrl && !beforeEmit) {
1303+
const shouldNavigate = (redirectUrl || setActiveNavigate) && !beforeEmit;
1304+
if (shouldNavigate) {
13131305
await tracker.track(async () => {
13141306
if (!this.client) {
13151307
// Typescript is not happy because since thinks this.client might have changed to undefined because the function is asynchronous.
13161308
return;
13171309
}
1318-
this.#setTransitiveState();
1319-
if (this.client.isEligibleForTouch()) {
1320-
const absoluteRedirectUrl = new URL(redirectUrl, window.location.href);
1321-
await this.navigate(this.buildUrlWithAuth(this.client.buildTouchUrl({ redirectUrl: absoluteRedirectUrl })));
1322-
} else {
1310+
1311+
if (sessionIsPending) {
1312+
this.#setTransitiveState();
1313+
}
1314+
1315+
if (setActiveNavigate) {
1316+
await setActiveNavigate();
1317+
return;
1318+
}
1319+
1320+
const taskUrl =
1321+
sessionIsPending && this.session?.currentTask && this.#options.taskUrls?.[this.session?.currentTask.key];
1322+
if (taskUrl) {
1323+
await this.navigate(taskUrl);
1324+
return;
1325+
}
1326+
1327+
if (!redirectUrl) {
1328+
return;
1329+
}
1330+
1331+
if (!this.client.isEligibleForTouch()) {
13231332
await this.navigate(redirectUrl);
1333+
return;
13241334
}
1335+
1336+
const absoluteRedirectUrl = new URL(redirectUrl, window.location.href);
1337+
const redirectUrlWithAuth = this.buildUrlWithAuth(
1338+
this.client.buildTouchUrl({ redirectUrl: absoluteRedirectUrl }),
1339+
);
1340+
await this.navigate(redirectUrlWithAuth);
13251341
});
13261342
}
13271343

@@ -1332,54 +1348,14 @@ export class Clerk implements ClerkInterface {
13321348

13331349
this.#setAccessors(newSession);
13341350
this.#emit();
1335-
await onAfterSetActive();
1336-
} finally {
1337-
this.__internal_setActiveInProgress = false;
1338-
}
1339-
};
1340-
1341-
#handlePendingSession = async (session: PendingSessionResource, onPendingSession?: OnPendingSessionFn) => {
1342-
if (!this.environment) {
1343-
return;
1344-
}
1345-
1346-
let currentSession: SignedInSessionResource | null = session;
1347-
1348-
if (inActiveBrowserTab() || !this.#options.standardBrowser) {
1349-
// Handles multi-session scenario when switching between `pending` sessions
1350-
// and satisfying task requirements such as organization selection
1351-
await this.#touchCurrentSession(session);
1352-
currentSession = this.#getSessionFromClient(session.id) ?? session;
1353-
}
1354-
1355-
// Syncs __session and __client_uat, in case the `pending` session
1356-
// has expired, it needs to trigger a sign-out
1357-
const token = await session.getToken();
1358-
if (!token) {
1359-
eventBus.emit(events.TokenUpdate, { token: null });
1360-
}
13611351

1362-
if (currentSession.status === 'pending') {
1363-
const tracker = createBeforeUnloadTracker(this.#options.standardBrowser);
1364-
1365-
const onPendingSessionHook = onPendingSession ?? this.#options['onPendingSession'];
1366-
const taskUrls = this.#options['taskUrls'];
1367-
1368-
await tracker.track(async () => {
1369-
if (onPendingSessionHook) {
1370-
await onPendingSessionHook({ session: currentSession });
1371-
} else if (taskUrls) {
1372-
await this.navigate(taskUrls[session.currentTask.key]);
1373-
}
1374-
});
1375-
1376-
if (tracker.isUnloading()) {
1377-
return;
1352+
// Do not revalidate server cache for pending sessions to avoid unmount of `SignIn/SignUp` AIOs when navigating to task
1353+
if (!sessionIsPending) {
1354+
await onAfterSetActive();
13781355
}
1356+
} finally {
1357+
this.__internal_setActiveInProgress = false;
13791358
}
1380-
1381-
this.#setAccessors(currentSession);
1382-
this.#emit();
13831359
};
13841360

13851361
public addListener = (listener: ListenerCallback): UnsubscribeCallback => {
@@ -2370,8 +2346,8 @@ export class Clerk implements ClerkInterface {
23702346
this.#authService = await AuthCookieService.create(
23712347
this,
23722348
this.#fapiClient,
2373-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2374-
this.#instanceType!,
2349+
2350+
this.#instanceType,
23752351
this.#publicEventBus,
23762352
);
23772353

packages/types/src/clerk.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,11 +1069,6 @@ export type ClerkOptions = ClerkOptionsNavigation &
10691069
* @default undefined - Uses Clerk's default task flow URLs
10701070
*/
10711071
taskUrls?: Record<SessionTask['key'], string>;
1072-
1073-
/**
1074-
* Event handler called when a session transitions to `pending` status after successful sign-in.
1075-
*/
1076-
onPendingSession?: OnPendingSessionFn;
10771072
};
10781073

10791074
export interface NavigateOptions {
@@ -1200,9 +1195,9 @@ export type SetActiveParams = {
12001195
redirectUrl?: string;
12011196

12021197
/**
1203-
* Event handler called when a session transitions to `pending` status after successful sign-in.
1198+
* TODO
12041199
*/
1205-
onPendingSession?: OnPendingSessionFn;
1200+
navigate?: SetActiveNavigate;
12061201
};
12071202

12081203
/**

0 commit comments

Comments
 (0)