Skip to content

Commit d952599

Browse files
committed
Pass treatPendingAsSignedOut to auth on clerkMiddleware
1 parent f9fb0f6 commit d952599

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

.changeset/sixty-carpets-stare.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
'@clerk/astro': patch
3+
---
4+
5+
Introduce `treatPendingAsSignedOut` option to `getAuth` and `auth` from `clerkMiddleware`
6+
7+
```ts
8+
const { userId } = getAuth(req, locals, { treatPendingAsSignedOut: false })
9+
```
10+
11+
```ts
12+
clerkMiddleware((auth, context) => {
13+
const { redirectToSignIn, userId } = auth({ treatPendingAsSignedOut: false })
14+
15+
// Both `active` and `pending` sessions will be treated as authenticated when `treatPendingAsSignedOut` is false
16+
if (!userId && isProtectedRoute(context.request)) {
17+
return redirectToSignIn()
18+
}
19+
})
20+
```

packages/astro/src/server/clerk-middleware.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isDevelopmentFromSecretKey } from '@clerk/shared/keys';
55
import { handleNetlifyCacheInDevInstance } from '@clerk/shared/netlifyCacheHandler';
66
import { isHttpOrHttps } from '@clerk/shared/proxy';
77
import { handleValueOrFn } from '@clerk/shared/utils';
8+
import type { PendingSessionOptions } from '@clerk/types';
89
import type { APIContext } from 'astro';
910

1011
import { authAsyncStorage } from '#async-local-storage';
@@ -244,8 +245,8 @@ function decorateAstroLocal(clerkRequest: ClerkRequest, context: APIContext, req
244245
context.locals.authStatus = status;
245246
context.locals.authMessage = message;
246247
context.locals.authReason = reason;
247-
context.locals.auth = () => {
248-
const authObject = getAuth(clerkRequest, context.locals);
248+
context.locals.auth = ({ treatPendingAsSignedOut }: PendingSessionOptions = {}) => {
249+
const authObject = getAuth(clerkRequest, context.locals, { treatPendingAsSignedOut });
249250

250251
const clerkUrl = clerkRequest.clerkUrl;
251252

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,16 @@ export const createGetAuth = ({ noAuthStatusMessage }: { noAuthStatusMessage: st
3636
authReason,
3737
};
3838

39-
let authObject;
40-
4139
if (authStatus !== AuthStatus.SignedIn) {
42-
authObject = signedOutAuthObject(options);
40+
return signedOutAuthObject(options);
4341
}
4442

4543
const jwt = decodeJwt(authToken as string);
46-
// @ts-expect-error -- Restrict parameter type of options to only list what's needed
47-
authObject = signedInAuthObject(options, jwt.raw.text, jwt.payload);
44+
// @ts-expect-error - TODO: Align types
45+
const authObject = signedInAuthObject(options, jwt.raw.text, jwt.payload);
4846

4947
if (treatPendingAsSignedOut && authObject.sessionStatus === 'pending') {
50-
authObject = signedOutAuthObject(options);
48+
return signedOutAuthObject(options);
5149
}
5250

5351
return authObject;

0 commit comments

Comments
 (0)