Skip to content

Commit 00525a0

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

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
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, { 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

0 commit comments

Comments
 (0)