I'm playing around with the new clerkMiddleware and trying to see if there would be a way for me to check if a user has a certain role by calling convex and index the users table with the token and check if that user has the role we want?
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
const isHubRoute = createRouteMatcher(["/hub(.*)"]);
const isAdminRoute = createRouteMatcher(["/admin(.*)"]);
export default clerkMiddleware(
(auth, req) => {
// Restrict admin routes to users with administrative permissions
if (isAdminRoute(req)) {
auth().protect((has) => {
const token = auth().getToken();
// Call convex with the token and check for admin role
// ...
const isAdmin = checkAdminRole(token);
return isAdmin; // Adjust permission identifier as needed
});
} else if (isHubRoute(req)) {
auth().protect();
}
},
{ debug: true }
);
// Config matcher might still be used to apply this middleware correctly based on your app's routing
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};