Skip to content

Commit 7537036

Browse files
committed
Resolve auth object with options from handler
1 parent 2ca6ac5 commit 7537036

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

packages/nextjs/src/server/clerkMiddleware.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { AuthObject, ClerkClient } from '@clerk/backend';
22
import type {
3+
AuthenticatedState,
34
AuthenticateRequestOptions,
45
ClerkRequest,
56
RedirectFun,
67
RequestState,
78
SignedInAuthObject,
89
SignedOutAuthObject,
10+
UnauthenticatedState,
911
} from '@clerk/backend/internal';
1012
import {
1113
AuthStatus,
@@ -214,7 +216,7 @@ export const clerkMiddleware = ((...args: unknown[]): NextMiddleware | NextMiddl
214216
const redirectToSignUp = createMiddlewareRedirectToSignUp(clerkRequest);
215217
const protect = await createMiddlewareProtect(clerkRequest, authObject, redirectToSignIn);
216218

217-
const authHandler = createMiddlewareAuthHandler(authObject, redirectToSignIn, redirectToSignUp);
219+
const authHandler = createMiddlewareAuthHandler(requestState, redirectToSignIn, redirectToSignUp);
218220
authHandler.protect = protect;
219221

220222
let handlerResult: Response = NextResponse.next();
@@ -427,14 +429,18 @@ const createMiddlewareProtect = (
427429
* - For machine tokens: validates token type and returns appropriate auth object
428430
*/
429431
const createMiddlewareAuthHandler = (
430-
rawAuthObject: AuthObject,
432+
requestState: AuthenticatedState<'session_token'> | UnauthenticatedState<'session_token'>,
431433
redirectToSignIn: RedirectFun<Response>,
432434
redirectToSignUp: RedirectFun<Response>,
433435
): ClerkMiddlewareAuth => {
434436
const authHandler = async (options?: GetAuthOptions) => {
437+
const rawAuthObject = requestState.toAuth({ treatPendingAsSignedOut: options?.treatPendingAsSignedOut });
435438
const acceptsToken = options?.acceptsToken ?? TokenType.SessionToken;
436439

437-
const authObject = getAuthObjectForAcceptedToken({ authObject: rawAuthObject, acceptsToken });
440+
const authObject = getAuthObjectForAcceptedToken({
441+
authObject: rawAuthObject,
442+
acceptsToken,
443+
});
438444

439445
if (authObject.tokenType === TokenType.SessionToken && isTokenTypeAccepted(TokenType.SessionToken, acceptsToken)) {
440446
return Object.assign(authObject, {

0 commit comments

Comments
 (0)