-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0da928
commit 8de8a4e
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' | ||
} | ||
}); | ||
``` |