Skip to content

Commit 8af4d06

Browse files
committed
Refactor middleware to implement route protection; utilize createRouteMatcher for defining protected routes and enhance clerkMiddleware for improved authentication handling.
1 parent 4d73a80 commit 8af4d06

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

src/middleware.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
import { clerkMiddleware } from "@clerk/nextjs/server";
1+
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';
22

3-
export default clerkMiddleware();
3+
const isProtectedRoute = createRouteMatcher([
4+
'/auth-success',
5+
'/generate(.*)',
6+
'/upload',
7+
'/remove-background',
8+
'/transform(.*)',
9+
]);
10+
11+
export default clerkMiddleware(async (auth, req) => {
12+
if (isProtectedRoute(req)) await auth.protect();
13+
});
414

515
export const config = {
6-
matcher: [
7-
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
8-
"/(api|trpc)(.*)",
9-
],
16+
matcher: [
17+
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
18+
'/(api|trpc)(.*)',
19+
],
1020
};

0 commit comments

Comments
 (0)