11import type { AuthObject } from '@clerk/backend' ;
22import { AuthStatus , signedInAuthObject , signedOutAuthObject } from '@clerk/backend/internal' ;
33import { decodeJwt } from '@clerk/backend/jwt' ;
4+ import type { PendingSessionOptions } from '@clerk/types' ;
45import type { APIContext } from 'astro' ;
56
67import { getSafeEnv } from './get-safe-env' ;
@@ -9,7 +10,11 @@ import { getAuthKeyFromRequest } from './utils';
910export type GetAuthReturn = AuthObject ;
1011
1112export const createGetAuth = ( { noAuthStatusMessage } : { noAuthStatusMessage : string } ) => {
12- return ( req : Request , locals : APIContext [ 'locals' ] , opts ?: { secretKey ?: string } ) : GetAuthReturn => {
13+ return (
14+ req : Request ,
15+ locals : APIContext [ 'locals' ] ,
16+ { treatPendingAsSignedOut = true , ...opts } : { secretKey ?: string } & PendingSessionOptions = { } ,
17+ ) : GetAuthReturn => {
1318 // When the auth status is set, we trust that the middleware has already run
1419 // Then, we don't have to re-verify the JWT here,
1520 // we can just strip out the claims manually.
@@ -31,13 +36,21 @@ export const createGetAuth = ({ noAuthStatusMessage }: { noAuthStatusMessage: st
3136 authReason,
3237 } ;
3338
39+ let authObject ;
40+
3441 if ( authStatus !== AuthStatus . SignedIn ) {
35- return signedOutAuthObject ( options ) ;
42+ authObject = signedOutAuthObject ( options ) ;
3643 }
3744
3845 const jwt = decodeJwt ( authToken as string ) ;
39- // @ts -expect-error - TODO: Align types
40- return signedInAuthObject ( options , jwt . raw . text , jwt . payload ) ;
46+ // @ts -expect-error -- Restrict parameter type of options to only list what's needed
47+ authObject = signedInAuthObject ( options , jwt . raw . text , jwt . payload ) ;
48+
49+ if ( treatPendingAsSignedOut && authObject . sessionStatus === 'pending' ) {
50+ authObject = signedOutAuthObject ( options ) ;
51+ }
52+
53+ return authObject ;
4154 } ;
4255} ;
4356
0 commit comments