-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.js
More file actions
51 lines (44 loc) · 1.29 KB
/
middleware.js
File metadata and controls
51 lines (44 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';
// Define protected routes that require authentication
const isProtectedRoute = createRouteMatcher([
'/dashboard(.*)',
'/create-course(.*)'
]);
// Public routes that don't require authentication
const publicRoutes = [
'/',
'/course/[courseId]',
'/course/[courseId]/start',
'/api/webhook/clerk',
'/api/uploadthing',
'/sign-in(.*)',
'/sign-up(.*)',
'/api/trpc(.*)'
];
export default clerkMiddleware((auth, req) => {
const { pathname } = req.nextUrl;
// Skip middleware for public routes
if (publicRoutes.some(route => {
if (route.includes('[') && route.includes(']')) {
// Handle dynamic routes like /course/[courseId]
const basePath = route.split('[')[0];
return pathname.startsWith(basePath);
}
return pathname === route || pathname.startsWith(route);
})) {
return null;
}
// For protected routes, require authentication
if (isProtectedRoute(req)) {
auth.protect();
}
return null;
});
export const config = {
matcher: [
// Skip Next.js internals and all static files, unless found in search params
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
// Always run for API routes
'/(api|trpc)(.*)',
],
};