-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
29 lines (25 loc) · 861 Bytes
/
middleware.ts
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
import { NextResponse } from "next/server";
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
const isAuthenticatedRoute = createRouteMatcher([
"/account",
"/admin/(.*)",
"/profile",
"/settings",
]);
const isAdminRoute = createRouteMatcher(["/admin/(.*)"]);
export default clerkMiddleware((auth, req) => {
// Restrict admin routes to users that are signed in and have the admin role
if (isAdminRoute(req)) {
if (auth().sessionClaims?.metadata.role !== "admin") {
const url = req.nextUrl.clone();
url.pathname = "/";
return NextResponse.rewrite(url);
}
}
// Restrict organization routes to users that are signed in
if (isAuthenticatedRoute(req)) auth().protect();
return NextResponse.next();
});
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};