Skip to content

Commit

Permalink
chore: add changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Jan 30, 2025
1 parent b0da928 commit 8de8a4e
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .changeset/sour-foxes-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
'@clerk/nuxt': minor
---

Add `createRouteMatcher()`, a helper function that allows you to protect multiple pages or API routes.

For protecting pages (in a global route middleware):

```ts
// createRouteMatcher is automatically imported
const isProtectedRoute = createRouteMatcher(['/dashboard(.*)', '/forum(.*)'])

export default defineNuxtRouteMiddleware(to => {
const { userId } = useAuth();

if (userId.value && isProtectedRoute(to)) {
return navigateTo('/sign-in');
}
});
```

For protecting API routes:

```ts
import { clerkMiddleware, createRouteMatcher } from '@clerk/nuxt/server';

// Unlike pages, you need to import `createRouteMatcher` from `@clerk/nuxt/server`
const isProtectedRoute = createRouteMatcher(['/api/user(.*)', '/api/projects(.*)']);

export default clerkMiddleware((event) => {
const { userId } = event.context.auth

if (!userId && isProtectedRoute(event)) {
setResponseStatus(event, 401)
return 'You are not authorized to access this resource.'
}
});
```

0 comments on commit 8de8a4e

Please sign in to comment.