Clerk middleware not protecting routes: 6 causes and a fail-closed setup (proofforge.live)
You added Clerk to a Next.js app, signed in, and the dashboard works. Then you open the same URL in a private window and it loads anyway.
That is the most common Clerk middleware surprise, and it is easy to miss when an AI tool wrote the setup. I'm part of the ProofForge team, and this is the check I'd run before shipping.
Why it happens
In current Clerk versions, clerkMiddleware() makes auth available to your app but does not protect any route by default. If you only export the middleware and a matcher, every page is public until you say otherwise.
Check the version of @clerk/nextjs you use and read its middleware docs, since older authMiddleware behaved differently.
6 causes of "my route is not protected"
- No protect call. The middleware file exists but never calls
auth.protect()for any route. - A route matcher that misses the path.
createRouteMatcher(['/dashboard(.*)'])does not cover/adminor/settings. List every private prefix. - A config matcher that skips API routes. The default matcher should include
/(api|trpc)(.*). If it was edited to exclude them, your API handlers get no middleware at all. - Protection only in the UI. Hiding a button for signed-out users is not access control. The handler behind it still answers anyone who calls it.
- Trusting the middleware alone. Check
auth()inside the route handler or server action too, and check the user owns the record, not only that they are signed in. - Old or renamed file. Middleware must sit at the project root (or
src/) and be namedmiddleware.tsin the Next.js version you use. A misplaced file is silently ignored.
A minimal safe shape
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
const isPublic = createRouteMatcher(['/', '/sign-in(.*)', '/sign-up(.*)'])
export default clerkMiddleware(async (auth, req) => {
if (!isPublic(req)) await auth.protect()
})
export const config = {
matcher: [
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
'/(api|trpc)(.*)',
],
}
This flips the default: everything is private unless you list it as public. Failing closed is safer than remembering to list every private route.
Test it from outside
Don't trust the code read. Open a private window, signed out, and request each private page and API route. You want a redirect to sign-in or a 401, not data. Then sign in as a second user and try to fetch the first user's record by ID.
Don't "fix" it by loosening checks
If a route returns 401 to a legitimate user, check the session and the matcher. Don't make the route public to get past it.
Check your own app from the outside, free
Run the free ProofForge scan on your live URL. No account needed for the first scan. It looks at your public site the way a stranger would and says pass, fail or inconclusive, so you know what is confirmed and what it could not see.
One-time reports start at $9: see pricing. Building with an AI agent? There is an MCP server so it can check before it deploys.
Comments
No comments yet. Start the discussion.