@@ -2,6 +2,7 @@ import type { AuthObject, ClerkClient } from '@clerk/backend';
2
2
import type { AuthenticateRequestOptions , ClerkRequest , RedirectFun , RequestState } from '@clerk/backend/internal' ;
3
3
import { AuthStatus , constants , createClerkRequest , createRedirect } from '@clerk/backend/internal' ;
4
4
import { parsePublishableKey } from '@clerk/shared/keys' ;
5
+ import type { PendingSessionOptions } from '@clerk/types' ;
5
6
import { notFound as nextjsNotFound } from 'next/navigation' ;
6
7
import type { NextMiddleware , NextRequest } from 'next/server' ;
7
8
import { NextResponse } from 'next/server' ;
@@ -41,7 +42,7 @@ export type ClerkMiddlewareAuthObject = AuthObject & {
41
42
} ;
42
43
43
44
export interface ClerkMiddlewareAuth {
44
- ( ) : Promise < ClerkMiddlewareAuthObject > ;
45
+ ( opts ?: PendingSessionOptions ) : Promise < ClerkMiddlewareAuthObject > ;
45
46
46
47
protect : AuthProtect ;
47
48
}
@@ -182,11 +183,14 @@ export const clerkMiddleware = ((...args: unknown[]): NextMiddleware | NextMiddl
182
183
const redirectToSignUp = createMiddlewareRedirectToSignUp ( clerkRequest ) ;
183
184
const protect = await createMiddlewareProtect ( clerkRequest , authObject , redirectToSignIn ) ;
184
185
185
- const authObjWithMethods : ClerkMiddlewareAuthObject = Object . assign ( authObject , {
186
- redirectToSignIn,
187
- redirectToSignUp,
188
- } ) ;
189
- const authHandler = ( ) => Promise . resolve ( authObjWithMethods ) ;
186
+ const authHandler = ( opts ?: PendingSessionOptions ) => {
187
+ const authObjWithMethods : ClerkMiddlewareAuthObject = Object . assign ( requestState . toAuth ( opts ) , {
188
+ redirectToSignIn,
189
+ redirectToSignUp,
190
+ } ) ;
191
+
192
+ return Promise . resolve ( authObjWithMethods ) ;
193
+ } ;
190
194
authHandler . protect = protect ;
191
195
192
196
let handlerResult : Response = NextResponse . next ( ) ;
0 commit comments