@@ -28,9 +28,9 @@ import type {
28
28
AuthenticateWithGoogleOneTapParams ,
29
29
AuthenticateWithMetamaskParams ,
30
30
AuthenticateWithOKXWalletParams ,
31
- Clerk as ClerkInterface ,
32
31
ClerkAPIError ,
33
32
ClerkAuthenticateWithWeb3Params ,
33
+ Clerk as ClerkInterface ,
34
34
ClerkOptions ,
35
35
ClientJSONSnapshot ,
36
36
ClientResource ,
@@ -54,7 +54,6 @@ import type {
54
54
OrganizationProfileProps ,
55
55
OrganizationResource ,
56
56
OrganizationSwitcherProps ,
57
- PendingSessionResource ,
58
57
PricingTableProps ,
59
58
PublicKeyCredentialCreationOptionsWithoutExtensions ,
60
59
PublicKeyCredentialRequestOptionsWithoutExtensions ,
@@ -119,7 +118,7 @@ import {
119
118
stripOrigin ,
120
119
windowNavigate ,
121
120
} from '../utils' ;
122
- import { assertNoLegacyProp , warnNoTaskOptions } from '../utils/assertNoLegacyProp' ;
121
+ import { assertNoLegacyProp } from '../utils/assertNoLegacyProp' ;
123
122
import { CLERK_ENVIRONMENT_STORAGE_ENTRY , SafeLocalStorage } from '../utils/localStorage' ;
124
123
import { memoizeListenerCallback } from '../utils/memoizeStateListenerCallback' ;
125
124
import { RedirectUrls } from '../utils/redirectUrls' ;
@@ -1199,14 +1198,11 @@ export class Clerk implements ClerkInterface {
1199
1198
/**
1200
1199
* `setActive` can be used to set the active session and/or organization.
1201
1200
*/
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 ;
1209
1204
this . __internal_setActiveInProgress = true ;
1205
+
1210
1206
try {
1211
1207
if ( ! this . client ) {
1212
1208
throw new Error ( 'setActive is being called before the client is loaded. Wait for init.' ) ;
@@ -1218,6 +1214,10 @@ export class Clerk implements ClerkInterface {
1218
1214
) ;
1219
1215
}
1220
1216
1217
+ if ( typeof session === 'string' ) {
1218
+ session = ( this . client . sessions . find ( x => x . id === session ) as SignedInSessionResource ) || null ;
1219
+ }
1220
+
1221
1221
const onBeforeSetActive : SetActiveHook =
1222
1222
typeof window !== 'undefined' && typeof window . __unstable__onBeforeSetActive === 'function'
1223
1223
? window . __unstable__onBeforeSetActive
@@ -1228,11 +1228,8 @@ export class Clerk implements ClerkInterface {
1228
1228
? window . __unstable__onAfterSetActive
1229
1229
: noop ;
1230
1230
1231
- if ( typeof session === 'string' ) {
1232
- session = ( this . client . sessions . find ( x => x . id === session ) as SignedInSessionResource ) || null ;
1233
- }
1234
-
1235
1231
let newSession = session === undefined ? this . session : session ;
1232
+ const sessionIsPending = newSession ?. status === 'pending' ;
1236
1233
1237
1234
// At this point, the `session` variable should contain either an `SignedInSessionResource`
1238
1235
// ,`null` or `undefined`.
@@ -1263,20 +1260,14 @@ export class Clerk implements ClerkInterface {
1263
1260
}
1264
1261
}
1265
1262
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 ) ;
1273
1269
}
1274
1270
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
-
1280
1271
//1. setLastActiveSession to passed user session (add a param).
1281
1272
// Note that this will also update the session's active organization
1282
1273
// id.
@@ -1309,19 +1300,44 @@ export class Clerk implements ClerkInterface {
1309
1300
} ) ;
1310
1301
}
1311
1302
1312
- if ( redirectUrl && ! beforeEmit ) {
1303
+ const shouldNavigate = ( redirectUrl || setActiveNavigate ) && ! beforeEmit ;
1304
+ if ( shouldNavigate ) {
1313
1305
await tracker . track ( async ( ) => {
1314
1306
if ( ! this . client ) {
1315
1307
// Typescript is not happy because since thinks this.client might have changed to undefined because the function is asynchronous.
1316
1308
return ;
1317
1309
}
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 ( ) ) {
1323
1332
await this . navigate ( redirectUrl ) ;
1333
+ return ;
1324
1334
}
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 ) ;
1325
1341
} ) ;
1326
1342
}
1327
1343
@@ -1332,54 +1348,14 @@ export class Clerk implements ClerkInterface {
1332
1348
1333
1349
this . #setAccessors( newSession ) ;
1334
1350
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
- }
1361
1351
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 ( ) ;
1378
1355
}
1356
+ } finally {
1357
+ this . __internal_setActiveInProgress = false ;
1379
1358
}
1380
-
1381
- this . #setAccessors( currentSession ) ;
1382
- this . #emit( ) ;
1383
1359
} ;
1384
1360
1385
1361
public addListener = ( listener : ListenerCallback ) : UnsubscribeCallback => {
@@ -2370,8 +2346,8 @@ export class Clerk implements ClerkInterface {
2370
2346
this . #authService = await AuthCookieService . create (
2371
2347
this ,
2372
2348
this . #fapiClient,
2373
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2374
- this . #instanceType! ,
2349
+
2350
+ this . #instanceType,
2375
2351
this . #publicEventBus,
2376
2352
) ;
2377
2353
0 commit comments