import { clerkMiddleware } from "@clerk/nextjs/server";
import { createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
const isPrivateRoute = createRouteMatcher([
'/dashboard(.*)',
]);
const isPublicRoute = createRouteMatcher([
'/(.*)',
'/auth/sign-in(.*)',
'/auth/sign-up(.*)'
])
export default clerkMiddleware((auth, req) => {
if(isPrivateRoute(req)){
if(!auth().sessionId && !auth().userId){
auth().protect();
}
return NextResponse.next();
}
return NextResponse.next();
},{ debug:true});
export const config = {
matcher: ["/((?!.*\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};
I’m building a SaaS and I’m using Clerk to do user authentication. I use Middleware to check the logged in user’s sessionID to provide a private route. Even after the user is logged in, the middleware cannot get the sessionID and redirects, according to auth().protect().
New contributor
Victor Cordeiro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.