|
1 | 1 | # @clerk/astro |
2 | 2 |
|
| 3 | +## 4.0.0 |
| 4 | + |
| 5 | +### Major Changes |
| 6 | + |
| 7 | +- Add support for Astro 7 and drop support for Astro 4. If you are still on Astro 4, follow the [Astro upgrade guide](https://docs.astro.build/en/guides/upgrade-to/v6/) to upgrade your project before updating `@clerk/astro`. ([#8974](https://github.com/clerk/javascript/pull/8974)) by [@wobsoriano](https://github.com/wobsoriano) |
| 8 | + |
| 9 | +- Remove `createRouteMatcher` from `@clerk/astro/server`. Use resource-based auth checks instead: move auth checks into each Astro page, API route, or server-side handler that accesses protected data. Middleware-based auth checks rely on path matching, which can diverge from how Astro routes requests and leave protected resources reachable. ([#8974](https://github.com/clerk/javascript/pull/8974)) by [@wobsoriano](https://github.com/wobsoriano) |
| 10 | + |
| 11 | + ```ts |
| 12 | + import type { APIRoute } from 'astro'; |
| 13 | + |
| 14 | + export const GET: APIRoute = ({ locals }) => { |
| 15 | + const { userId } = locals.auth(); |
| 16 | + |
| 17 | + if (!userId) { |
| 18 | + return new Response('Unauthorized', { status: 401 }); |
| 19 | + } |
| 20 | + |
| 21 | + return Response.json({ userId }); |
| 22 | + }; |
| 23 | + ``` |
| 24 | + |
| 25 | + If you want to hand this work to a coding agent, use this migration prompt: |
| 26 | + |
| 27 | + ```md |
| 28 | + Migrate my Astro project away from Clerk's removed `createRouteMatcher` API. |
| 29 | + |
| 30 | + 1. Open `src/middleware.ts` (or `src/middleware.js`) and find every matcher created |
| 31 | + with `createRouteMatcher` from `@clerk/astro/server`, along with the middleware |
| 32 | + logic that uses it (returning 401s, calling `auth().redirectToSignIn()`, etc.). |
| 33 | + 2. For every route those matchers protected, move the auth check into the resource itself: |
| 34 | + - In `.astro` pages, add this to the frontmatter: |
| 35 | + const { userId, redirectToSignIn } = Astro.locals.auth(); |
| 36 | + if (!userId) return redirectToSignIn(); |
| 37 | + - In API routes and server handlers, add this at the top of the handler: |
| 38 | + const { userId } = locals.auth(); |
| 39 | + if (!userId) return new Response('Unauthorized', { status: 401 }); |
| 40 | + - Keep any role or permission checks (`auth().has(...)`) with the resource as well. |
| 41 | + 3. Remove the `createRouteMatcher` import and calls from the middleware. Keep |
| 42 | + `clerkMiddleware()` itself. Middleware logic unrelated to auth protection |
| 43 | + (locale redirects, headers, etc.) may stay, using plain `URL`/pathname checks. |
| 44 | + 4. Make sure every page and endpoint previously covered by a matcher pattern |
| 45 | + (including glob patterns like `/dashboard(.*)`) now has its own check, then |
| 46 | + verify the project builds. |
| 47 | + ``` |
| 48 | + |
3 | 49 | ## 3.4.20 |
4 | 50 |
|
5 | 51 | ### Patch Changes |
|
0 commit comments