-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (35 loc) · 998 Bytes
/
middleware.ts
File metadata and controls
41 lines (35 loc) · 998 Bytes
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
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from 'next/server';
// Define route matchers
const isProtectedRoute = createRouteMatcher([
"/teacher/(.*)",
"/courses/(.*)",
"/dashboard/(.*)"
]);
const isPublicRoute = createRouteMatcher([
"/sign-in(.*)",
"/sign-up(.*)",
"/api/webhook(.*)",
"/api/uploadthing(.*)"
]);
export default clerkMiddleware(async (auth, req) => {
// Check if it's a public route first
if (isPublicRoute(req)) {
return NextResponse.next();
}
// For all other routes (including protected and root), ensure user is authenticated
try {
await auth.protect();
return NextResponse.next();
} catch (error) {
const signInUrl = new URL('/sign-in', req.url);
signInUrl.searchParams.set('redirect_url', req.url);
return NextResponse.redirect(signInUrl);
}
});
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico).*)",
"/api/:path*"
]
};