Skip to content

Commit 795d253

Browse files
committed
Accept treatPendingAsSignedOut on auth
1 parent b487d03 commit 795d253

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

packages/nextjs/src/app-router/server/auth.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AuthObject } from '@clerk/backend';
22
import { constants, createClerkRequest, createRedirect, type RedirectFun } from '@clerk/backend/internal';
3+
import type { PendingSessionOptions } from '@clerk/types';
34
import { notFound, redirect } from 'next/navigation';
45

56
import { PUBLISHABLE_KEY, SIGN_IN_URL, SIGN_UP_URL } from '../../server/constants';
@@ -38,7 +39,7 @@ type Auth = AuthObject & {
3839
};
3940

4041
export interface AuthFn {
41-
(): Promise<Auth>;
42+
(options?: PendingSessionOptions): Promise<Auth>;
4243

4344
/**
4445
* `auth` includes a single property, the `protect()` method, which you can use in two ways:
@@ -68,7 +69,7 @@ export interface AuthFn {
6869
* - Only works on the server-side, such as in Server Components, Route Handlers, and Server Actions.
6970
* - Requires [`clerkMiddleware()`](https://clerk.com/docs/references/nextjs/clerk-middleware) to be configured.
7071
*/
71-
export const auth: AuthFn = async () => {
72+
export const auth: AuthFn = async ({ treatPendingAsSignedOut } = {}) => {
7273
// eslint-disable-next-line @typescript-eslint/no-require-imports
7374
require('server-only');
7475

@@ -89,7 +90,7 @@ export const auth: AuthFn = async () => {
8990
const authObject = await createAsyncGetAuth({
9091
debugLoggerName: 'auth()',
9192
noAuthStatusMessage: authAuthHeaderMissing('auth', await stepsBasedOnSrcDirectory()),
92-
})(request);
93+
})(request, { treatPendingAsSignedOut });
9394

9495
const clerkUrl = getAuthKeyFromRequest(request, 'ClerkUrl');
9596

packages/nextjs/src/server/createGetAuth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { AuthObject } from '@clerk/backend';
22
import { constants } from '@clerk/backend/internal';
33
import { isTruthy } from '@clerk/shared/underscore';
4+
import type { PendingSessionOptions } from '@clerk/types';
45

56
import { withLogger } from '../utils/debugLogger';
67
import { isNextWithUnstableServerActions } from '../utils/sdk-versions';
@@ -22,7 +23,7 @@ export const createAsyncGetAuth = ({
2223
noAuthStatusMessage: string;
2324
}) =>
2425
withLogger(debugLoggerName, logger => {
25-
return async (req: RequestLike, opts?: { secretKey?: string }): Promise<AuthObject> => {
26+
return async (req: RequestLike, opts?: { secretKey?: string } & PendingSessionOptions): Promise<AuthObject> => {
2627
if (isTruthy(getHeader(req, constants.Headers.EnableDebug))) {
2728
logger.enable();
2829
}

packages/nextjs/src/server/data/getAuthDataFromRequest.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { AuthObject } from '@clerk/backend';
22
import { AuthStatus, constants, signedInAuthObject, signedOutAuthObject } from '@clerk/backend/internal';
33
import { decodeJwt } from '@clerk/backend/jwt';
4+
import type { PendingSessionOptions } from '@clerk/types';
45

56
import type { LoggerNoCommit } from '../../utils/debugLogger';
67
import { API_URL, API_VERSION, PUBLISHABLE_KEY, SECRET_KEY } from '../constants';
@@ -14,7 +15,10 @@ import { assertTokenSignature, decryptClerkRequestData } from '../utils';
1415
*/
1516
export function getAuthDataFromRequest(
1617
req: RequestLike,
17-
opts: { secretKey?: string; logger?: LoggerNoCommit } = {},
18+
{
19+
treatPendingAsSignedOut = true,
20+
...opts
21+
}: { secretKey?: string; logger?: LoggerNoCommit } & PendingSessionOptions = {},
1822
): AuthObject {
1923
const authStatus = getAuthKeyFromRequest(req, 'AuthStatus');
2024
const authToken = getAuthKeyFromRequest(req, 'AuthToken');
@@ -35,6 +39,7 @@ export function getAuthDataFromRequest(
3539
authStatus,
3640
authMessage,
3741
authReason,
42+
treatPendingAsSignedOut,
3843
};
3944

4045
opts.logger?.debug('auth options', options);
@@ -53,5 +58,9 @@ export function getAuthDataFromRequest(
5358
authObject = signedInAuthObject(options, jwt.raw.text, jwt.payload);
5459
}
5560

61+
if (treatPendingAsSignedOut && authObject.sessionStatus === 'pending') {
62+
authObject = signedOutAuthObject(options);
63+
}
64+
5665
return authObject;
5766
}

0 commit comments

Comments
 (0)