1
1
import type { AuthObject } from '@clerk/backend' ;
2
2
import { AuthStatus , signedInAuthObject , signedOutAuthObject } from '@clerk/backend/internal' ;
3
3
import { decodeJwt } from '@clerk/backend/jwt' ;
4
+ import type { PendingSessionOptions } from '@clerk/types' ;
4
5
import type { APIContext } from 'astro' ;
5
6
6
7
import { getSafeEnv } from './get-safe-env' ;
@@ -9,7 +10,11 @@ import { getAuthKeyFromRequest } from './utils';
9
10
export type GetAuthReturn = AuthObject ;
10
11
11
12
export 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 => {
13
18
// When the auth status is set, we trust that the middleware has already run
14
19
// Then, we don't have to re-verify the JWT here,
15
20
// we can just strip out the claims manually.
@@ -31,13 +36,21 @@ export const createGetAuth = ({ noAuthStatusMessage }: { noAuthStatusMessage: st
31
36
authReason,
32
37
} ;
33
38
39
+ let authObject ;
40
+
34
41
if ( authStatus !== AuthStatus . SignedIn ) {
35
- return signedOutAuthObject ( options ) ;
42
+ authObject = signedOutAuthObject ( options ) ;
36
43
}
37
44
38
45
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 ;
41
54
} ;
42
55
} ;
43
56
0 commit comments