Skip to content

Commit 96741e1

Browse files
committed
Trigger Next.js hooks on session status transition
1 parent 6459f7d commit 96741e1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,6 +2245,8 @@ export class Clerk implements ClerkInterface {
22452245
}
22462246

22472247
updateClient = (newClient: ClientResource): void => {
2248+
console.log({ newClient, newStatus: newClient.sessions?.[0]?.status, currentStatus: this.session?.status });
2249+
22482250
if (!this.client) {
22492251
// This is the first time client is being
22502252
// set, so we also need to set session
@@ -2258,6 +2260,23 @@ export class Clerk implements ClerkInterface {
22582260
if (this.session) {
22592261
const session = this.#getSessionFromClient(this.session.id);
22602262

2263+
const hasTransitionedToPendingStatus = this.session.status === 'active' && session?.status === 'pending';
2264+
if (hasTransitionedToPendingStatus) {
2265+
const onBeforeSetActive: SetActiveHook =
2266+
typeof window !== 'undefined' && typeof window.__unstable__onBeforeSetActive === 'function'
2267+
? window.__unstable__onBeforeSetActive
2268+
: noop;
2269+
2270+
const onAfterSetActive: SetActiveHook =
2271+
typeof window !== 'undefined' && typeof window.__unstable__onAfterSetActive === 'function'
2272+
? window.__unstable__onAfterSetActive
2273+
: noop;
2274+
2275+
// Execute hooks to update server authentication context and trigger
2276+
// page protections in clerkMiddleware or server components
2277+
void Promise.resolve(onBeforeSetActive()).then(() => onAfterSetActive());
2278+
}
2279+
22612280
// Note: this might set this.session to null
22622281
this.#setAccessors(session);
22632282

0 commit comments

Comments
 (0)