Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .changeset/soft-astro-routes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@clerk/astro': patch
---

Deprecate `createRouteMatcher()` in favor of resource-based auth checks.

Instead of protecting routes only from middleware, move auth checks into each protected Astro page, API route, or server-side handler:

```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 });
};
```
27 changes: 27 additions & 0 deletions packages/astro/src/server/route-matcher.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { deprecated } from '@clerk/shared/deprecated';
import { createPathMatcher, type PathMatcherParam } from '@clerk/shared/pathMatcher';

export type RouteMatcherParam = PathMatcherParam;
Expand All @@ -9,8 +10,34 @@ export type RouteMatcherParam = PathMatcherParam;
* You can use glob patterns to match multiple routes or a function to match against the request object.
* Path patterns and regular expressions are supported, for example: `['/foo', '/bar(.*)'] or `[/^\/foo\/.*$/]`
* For more information, see: https://clerk.com/docs
*
* @deprecated This function will be removed in the next major version. 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.
*
* Instead of protecting routes only from middleware, protect the resource itself:
*
* ```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 });
* };
* ```
*/
export const createRouteMatcher = (routes: RouteMatcherParam) => {
deprecated(
'createRouteMatcher',
'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.',
);

const matcher = createPathMatcher(routes);
return (req: Request) => matcher(new URL(req.url).pathname);
};
Comment on lines 35 to 43

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add an explicit return type for the exported API.

createRouteMatcher is exported but currently relies on inference. Please add an explicit function return type on Line 35 to keep the public surface unambiguous.

Suggested change
-export const createRouteMatcher = (routes: RouteMatcherParam) => {
+export const createRouteMatcher = (
+  routes: RouteMatcherParam,
+): ((req: Request) => boolean) => {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const createRouteMatcher = (routes: RouteMatcherParam) => {
deprecated(
'createRouteMatcher',
'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.',
);
const matcher = createPathMatcher(routes);
return (req: Request) => matcher(new URL(req.url).pathname);
};
export const createRouteMatcher = (
routes: RouteMatcherParam,
): ((req: Request) => boolean) => {
deprecated(
'createRouteMatcher',
'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.',
);
const matcher = createPathMatcher(routes);
return (req: Request) => matcher(new URL(req.url).pathname);
};
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/astro/src/server/route-matcher.ts` around lines 35 - 43, The
exported createRouteMatcher API currently relies on inferred return type, which
makes the public contract less explicit. Add an explicit return type to
createRouteMatcher and ensure it matches the function returned by
createPathMatcher, keeping the exported matcher signature clear and stable for
consumers.

Sources: Coding guidelines, Learnings

Loading