Next.js Discord

Discord Forum

Protect Dynamic Router using Nextjs Middleware

Answered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
I am trying to Protect Dynamic router like example/[id]/page.tsx using example/:path* but seems like it not working
Answered by fuma
and you made a mistake, nextUrl.pathname doesn’t equal to /something/:id*
View full answer

19 Replies

Because you have other patterns conflict with that such as /vendor/home, removing them should fix the error
Cape lionOP
can you please give me a bit detail about this i used middelwae for first Time and not aware of this , according to docs i am working fine but result is not expected
/vendor/home is for vendor dashboard i can't remove what i actually want i want to protect all router start with /admin and /vendor thats it
is it possible to do this with just one route mentioned?
/admin/:router* doesn't work at all
this doesn't work though
Because you already have /vendor/:path* above, it matches /vendor/home which means you don’t have to add that thing
Same for all other matchers starting with /vendor
Cape lionOP
got it let me try this out
correct now right ?
Yes
Cape lionOP
Everything is protected using /admin/:path* but on this route middleware run but couldn't catch this route and show me even when i am logged out
checkour terminal i refersh page and middelware run but unable to detect while it work complete fine for pages like /admin/home , /admin/profile even for through like /admin/vendor/pending-product/page.tsx but not for /admin/vendor/pending-product-details/123
It’s probably a problem with your code, check the middleware because I saw there’re some hardcoded if loops
and you made a mistake, nextUrl.pathname doesn’t equal to /something/:id*
Answer
It’s the full pathname in the url, you should match it with functions like startsWith
pathname.startsWith(“/admin”) makes more sense
Cape lionOP
works ......................................
thanks alot