-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
52 lines (43 loc) · 1.31 KB
/
Copy pathproxy.ts
File metadata and controls
52 lines (43 loc) · 1.31 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
52
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
// Public routes
const isPublicRoute = createRouteMatcher([
"/",
"/sign-in(.*)",
"/sign-up(.*)",
"/api/webhooks(.*)",
]);
// Admin routes
const isAdminRoute = createRouteMatcher([
"/admin(.*)",
]);
export default clerkMiddleware(async (auth, req) => {
const { userId, sessionClaims } = await auth();
// Allow public routes
if (isPublicRoute(req)) {
return NextResponse.next();
}
// Protect all non-public routes
if (!userId) {
const signInUrl = new URL('/sign-in', req.url);
signInUrl.searchParams.set('redirect_url', req.url);
return NextResponse.redirect(signInUrl);
}
// Check admin routes - only allow users with admin role
if (isAdminRoute(req)) {
const role = sessionClaims?.metadata?.role as string;
if (role !== 'admin') {
// Redirect non-admin users to dashboard
return NextResponse.redirect(new URL('/dashboard', req.url));
}
}
return NextResponse.next();
});
export const config = {
matcher: [
// Protect everything except static files
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
// Protect API routes too
"/(api|trpc)(.*)",
],
};