Skip to content

Commit 92dc3d3

Browse files
committed
Introduce treatPendingAsSignedOut to getAuth
1 parent 3d7384f commit 92dc3d3

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

packages/astro/src/server/get-auth.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { AuthObject } from '@clerk/backend';
22
import { AuthStatus, signedInAuthObject, signedOutAuthObject } from '@clerk/backend/internal';
33
import { decodeJwt } from '@clerk/backend/jwt';
4+
import type { PendingSessionOptions } from '@clerk/types';
45
import type { APIContext } from 'astro';
56

67
import { getSafeEnv } from './get-safe-env';
@@ -9,7 +10,11 @@ import { getAuthKeyFromRequest } from './utils';
910
export type GetAuthReturn = AuthObject;
1011

1112
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 => {
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

Comments
 (0)