full-stack-school/src/middleware.ts
2026-03-01 18:32:49 +00:00

34 lines
1.0 KiB
TypeScript

import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { routeAccessMap } from "./lib/settings";
import { NextResponse } from "next/server";
const matchers = Object.keys(routeAccessMap).map((route) => ({
matcher: createRouteMatcher([route]),
allowedRoles: routeAccessMap[route],
}));
console.log(matchers);
export default clerkMiddleware((auth, req) => {
// if (isProtectedRoute(req)) auth().protect()
const { sessionClaims } = auth();
const role = (sessionClaims?.metadata as { role?: string })?.role;
for (const { matcher, allowedRoles } of matchers) {
if (matcher(req) && !allowedRoles.includes(role!)) {
return NextResponse.redirect(new URL(`/${role}`, req.url));
}
}
});
export const config = {
matcher: [
// Skip Next.js internals and all static files, unless found in search params
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
// Always run for API routes
"/(api|trpc)(.*)",
],
};