-
Notifications
You must be signed in to change notification settings - Fork 461
feat(astro): Remove createRouteMatcher and support Astro 7 #8974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
393cc26
2037de9
9ee675c
fecc5e8
8432439
8d1a376
2db93cb
a72ad23
71670aa
56bc849
fb781f7
afe16c3
15586a3
1357f5c
5499148
2cb9fa4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@clerk/astro": major | ||
| --- | ||
|
|
||
| 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`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| --- | ||
| '@clerk/astro': major | ||
| --- | ||
|
|
||
| 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. | ||
|
|
||
| ```ts | ||
| import type { APIRoute } from 'astro'; | ||
|
|
||
| export const GET: APIRoute = ({ locals }) => { | ||
| const { userId } = locals.auth(); | ||
|
|
||
| if (!userId) { | ||
| return new Response('Unauthorized', { status: 401 }); | ||
| } | ||
|
|
||
| return Response.json({ userId }); | ||
| }; | ||
| ``` | ||
|
|
||
| If you want to hand this work to a coding agent, use this migration prompt: | ||
|
|
||
| ```md | ||
| Migrate my Astro project away from Clerk's removed `createRouteMatcher` API. | ||
|
|
||
| 1. Open `src/middleware.ts` (or `src/middleware.js`) and find every matcher created | ||
| with `createRouteMatcher` from `@clerk/astro/server`, along with the middleware | ||
| logic that uses it (returning 401s, calling `auth().redirectToSignIn()`, etc.). | ||
| 2. For every route those matchers protected, move the auth check into the resource itself: | ||
| - In `.astro` pages, add this to the frontmatter: | ||
| const { userId, redirectToSignIn } = Astro.locals.auth(); | ||
| if (!userId) return redirectToSignIn(); | ||
| - In API routes and server handlers, add this at the top of the handler: | ||
| const { userId } = locals.auth(); | ||
| if (!userId) return new Response('Unauthorized', { status: 401 }); | ||
| - Keep any role or permission checks (`auth().has(...)`) with the resource as well. | ||
| 3. Remove the `createRouteMatcher` import and calls from the middleware. Keep | ||
| `clerkMiddleware()` itself. Middleware logic unrelated to auth protection | ||
| (locale redirects, headers, etc.) may stay, using plain `URL`/pathname checks. | ||
| 4. Make sure every page and endpoint previously covered by a matcher pattern | ||
| (including glob patterns like `/dashboard(.*)`) now has its own check, then | ||
| verify the project builds. | ||
| ``` |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -262,6 +262,7 @@ export const registerApiKeyAuthTests = (adapter: MachineAuthTestAdapter): void = | |
| test('should handle multiple token types', async ({ page, context }) => { | ||
| const u = createTestUtils({ app, page, context }); | ||
| const url = new URL(adapter.apiKey.path, app.serverUrl).toString(); | ||
| const origin = new URL(app.serverUrl).origin; | ||
|
|
||
| await u.po.signIn.goTo(); | ||
| await u.po.signIn.waitForMounted(); | ||
|
|
@@ -271,14 +272,16 @@ export const registerApiKeyAuthTests = (adapter: MachineAuthTestAdapter): void = | |
| const getRes = await u.page.request.get(url); | ||
| expect(getRes.status()).toBe(401); | ||
|
|
||
| const postWithSessionRes = await u.page.request.post(url); | ||
| const postWithSessionRes = await u.page.request.post(url, { | ||
| headers: { Origin: origin }, | ||
| }); | ||
| const sessionData = await postWithSessionRes.json(); | ||
| expect(postWithSessionRes.status()).toBe(200); | ||
| expect(sessionData.userId).toBe(fakeBapiUser.id); | ||
| expect(sessionData.tokenType).toBe(TokenType.SessionToken); | ||
|
Comment on lines
+275
to
281
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Assert response status before parsing JSON bodies in POST branches.
Suggested patch const postWithSessionRes = await u.page.request.post(url, {
headers: { Origin: origin },
});
- const sessionData = await postWithSessionRes.json();
expect(postWithSessionRes.status()).toBe(200);
+ const sessionData = await postWithSessionRes.json();
expect(sessionData.userId).toBe(fakeBapiUser.id);
expect(sessionData.tokenType).toBe(TokenType.SessionToken);
const postWithApiKeyRes = await u.page.request.post(url, {
headers: { Authorization: `Bearer ${fakeAPIKey.secret}`, Origin: origin },
});
- const apiKeyData = await postWithApiKeyRes.json();
expect(postWithApiKeyRes.status()).toBe(200);
+ const apiKeyData = await postWithApiKeyRes.json();
expect(apiKeyData.userId).toBe(fakeBapiUser.id);
expect(apiKeyData.tokenType).toBe(TokenType.ApiKey);Also applies to: 283-285 🤖 Prompt for AI Agents |
||
|
|
||
| const postWithApiKeyRes = await u.page.request.post(url, { | ||
| headers: { Authorization: `Bearer ${fakeAPIKey.secret}` }, | ||
| headers: { Authorization: `Bearer ${fakeAPIKey.secret}`, Origin: origin }, | ||
| }); | ||
| const apiKeyData = await postWithApiKeyRes.json(); | ||
| expect(postWithApiKeyRes.status()).toBe(200); | ||
|
|
||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Astro v6 compatibility smoke test |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
|
|
||
| import type { Application } from '../../models/application'; | ||
| import { appConfigs } from '../../presets'; | ||
|
|
||
| test.describe('Astro version compatibility @astro', () => { | ||
| test.describe.configure({ mode: 'serial' }); | ||
|
|
||
| let app: Application; | ||
|
|
||
| test.beforeAll(async () => { | ||
| test.setTimeout(120_000); | ||
|
|
||
| app = await appConfigs.astro.node | ||
| .clone() | ||
| .setName('astro-node-v6-smoke') | ||
| .addDependency('astro', '^6.4.8') | ||
| .addDependency('@astrojs/node', '^10.1.4') | ||
| .addDependency('@astrojs/react', '^5.0.7') | ||
| .commit(); | ||
|
|
||
| await app.setup(); | ||
| await app.withEnv(appConfigs.envs.withCustomRoles); | ||
| }); | ||
|
|
||
| test.afterAll(async () => { | ||
| await app.teardown(); | ||
| }); | ||
|
|
||
| test('builds with Astro 6 and custom UserButton menu items', async () => { | ||
| await app.build(); | ||
|
|
||
| expect(app.buildOutput).not.toHaveLength(0); | ||
| expect(app.buildOutput).not.toContain('Illegal return statement'); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont need tailwind in this...