Skip to content

Commit e9134ec

Browse files
committed
Pass treatPendingAsSignedOut to server components on Next
1 parent 795d253 commit e9134ec

File tree

7 files changed

+35
-15
lines changed

7 files changed

+35
-15
lines changed

.changeset/tender-brooms-look.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
'@clerk/nextjs': patch
3+
---
4+
5+
Introduce `treatPendingAsSignedOut` to `auth` and server-side control components
6+
7+
```ts
8+
const { userId } = auth({ treatPendingAsSignedOut: false })
9+
```
10+
11+
```tsx
12+
<SignedIn treatPendingAsSignedOut={false}>
13+
User has a session that is either pending (requires tasks resolution) or active
14+
</SignedIn>
15+
```

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,7 @@ describe('Clerk singleton', () => {
22842284
await sut.load(mockedLoadOptions);
22852285

22862286
await sut.setActive({ session: mockResource as any as PendingSessionResource });
2287-
await sut.__experimental_navigateToTask();
2287+
await sut.__experimental_nextTask();
22882288

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

23302330
const redirectUrlComplete = '/welcome-to-app';
2331-
await sut.__experimental_navigateToTask({ redirectUrlComplete });
2331+
await sut.__experimental_nextTask({ redirectUrlComplete });
23322332

23332333
console.log(mockNavigate.mock.calls);
23342334

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ export class Clerk implements ClerkInterface {
11881188
this.#emit();
11891189
};
11901190

1191-
public __experimental_navigateToTask = async ({ redirectUrlComplete }: NextTaskParams = {}): Promise<void> => {
1191+
public __experimental_nextTask = async ({ redirectUrlComplete }: NextTaskParams = {}): Promise<void> => {
11921192
const session = await this.session?.reload();
11931193
if (!session || !this.environment) {
11941194
return;
@@ -1205,7 +1205,7 @@ export class Clerk implements ClerkInterface {
12051205
}
12061206

12071207
const tracker = createBeforeUnloadTracker(this.#options.standardBrowser);
1208-
const defaultRedirectUrlComplete = this.client?.signUp ? this.buildAfterSignUpUrl() : this.buildAfterSignInUrl();
1208+
const defaultRedirectUrlComplete = this.client?.signUp ? this.buildAfterSignUpUrl() : this.buildAfterSignUpUrl();
12091209

12101210
this.#setTransitiveState();
12111211

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const SessionTasksStart = withCardStateProvider(() => {
2121
useEffect(() => {
2222
// Simulates additional latency to avoid a abrupt UI transition when navigating to the next task
2323
const timeoutId = setTimeout(() => {
24-
void clerk.__experimental_navigateToTask({ redirectUrlComplete });
24+
void clerk.__experimental_nextTask({ redirectUrlComplete });
2525
}, 500);
2626
return () => clearTimeout(timeoutId);
2727
}, [navigate, clerk, redirectUrlComplete]);
@@ -80,7 +80,7 @@ export function SessionTask(): JSX.Element {
8080
}, [clerk, navigate, redirectUrlComplete]);
8181

8282
const nextTask = useCallback(
83-
() => clerk.__experimental_navigateToTask({ redirectUrlComplete }),
83+
() => clerk.__experimental_nextTask({ redirectUrlComplete }),
8484
[clerk, redirectUrlComplete],
8585
);
8686

packages/nextjs/src/app-router/server/controlComponents.tsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import type { ProtectProps } from '@clerk/clerk-react';
2+
import type { PendingSessionOptions } from '@clerk/types';
23
import React from 'react';
34

45
import { auth } from './auth';
56

6-
export async function SignedIn(props: React.PropsWithChildren): Promise<React.JSX.Element | null> {
7+
export async function SignedIn(
8+
props: React.PropsWithChildren<PendingSessionOptions>,
9+
): Promise<React.JSX.Element | null> {
710
const { children } = props;
8-
const { userId } = await auth();
11+
const { userId } = await auth({ treatPendingAsSignedOut: props.treatPendingAsSignedOut });
912
return userId ? <>{children}</> : null;
1013
}
1114

12-
export async function SignedOut(props: React.PropsWithChildren): Promise<React.JSX.Element | null> {
15+
export async function SignedOut(
16+
props: React.PropsWithChildren<PendingSessionOptions>,
17+
): Promise<React.JSX.Element | null> {
1318
const { children } = props;
14-
const { userId } = await auth();
19+
const { userId } = await auth({ treatPendingAsSignedOut: props.treatPendingAsSignedOut });
1520
return userId ? null : <>{children}</>;
1621
}
1722

@@ -29,7 +34,7 @@ export async function SignedOut(props: React.PropsWithChildren): Promise<React.J
2934
*/
3035
export async function Protect(props: ProtectProps): Promise<React.JSX.Element | null> {
3136
const { children, fallback, ...restAuthorizedParams } = props;
32-
const { has, userId } = await auth();
37+
const { has, userId } = await auth({ treatPendingAsSignedOut: props.treatPendingAsSignedOut });
3338

3439
/**
3540
* Fallback to UI provided by user or `null` if authorization checks failed

packages/react/src/isomorphicClerk.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,9 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
693693
}
694694
};
695695

696-
__experimental_navigateToTask = async (params?: NextTaskParams): Promise<void> => {
696+
__experimental_nextTask = async (params?: NextTaskParams): Promise<void> => {
697697
if (this.clerkjs) {
698-
return this.clerkjs.__experimental_navigateToTask(params);
698+
return this.clerkjs.__experimental_nextTask(params);
699699
} else {
700700
return Promise.reject();
701701
}

packages/types/src/clerk.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,10 @@ export interface Clerk {
706706
/**
707707
* Navigates to the next task or redirects to completion URL.
708708
* If the current session has pending tasks, it navigates to the next task.
709-
* 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).
709+
* If all tasks are complete, it navigates to the provided completion URL.
710710
* @experimental
711711
*/
712-
__experimental_navigateToTask: (params?: NextTaskParams) => Promise<void>;
712+
__experimental_nextTask: (params?: NextTaskParams) => Promise<void>;
713713

714714
/**
715715
* This is an optional function.

0 commit comments

Comments
 (0)