Next.js Discord

Discord Forum

Middleware Matcher

Unanswered
Asian black bear posted this in #help-forum
Open in Discord
Asian black bearOP
Please tell me there's a better a way to do this

All my protected routes are inside a (dashboard) route group. I just want to match them.

But instead I have to configure all the other routes that are NOT the ones I need to protect

And now my images stop working if I don't put their path in there. (They are in the public/ folder witch doesn't match to any route`)

4 Replies

you can check this example
https://nextjs.org/docs/pages/building-your-application/authentication#authorization

you are supossed to make an array of protected and public routes
and depending on those you navigate
matcher is not meant to do that
Asian black bearOP
Thanks for replying, but I really didn't want to keep adding routes to the middleware for every new route :/
I would use paths like /dashboard/ and /auth/
and use startsWith() to clasify them and all inside those folders
const isOnAuth = pathname.startsWith("/auth/");
const isOnDashboard = pathname.startsWith("/dashboard/");

const protectedRoutes = [isOnDashboard, etc];

if (protectedRoutes.some((route) => route)) {
if no user redirect to auth
}