Next.js Discord

Discord Forum

Middleware match path like this (admin) folder?

Answered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
I have a folder named (admin) that has my admin stuff , I want to use middleware to protect all pages in the folder
https://nextjs.org/docs/app/building-your-application/routing/middleware

but based on the doc, it MUST start with /, does that mean it is not possible to protect the entire folder?

my folder structure is like this
(admin)
  -- stats
  ---- page.tsx
  -- logs
  ---- page.tsx
  ...


and I want to protect all these pages
Answered by Ray
yes but not a route group
View full answer

6 Replies

Transvaal lionOP
something like this does not work if (request.nextUrl.pathname.startsWith('/(admin)')) {{
@Transvaal lion something like this does not work `if (request.nextUrl.pathname.startsWith('/(admin)')) {{`
yeah we need to do this
if (["/stats", "/logs"].includes(request.nextUrl.pathname))
Transvaal lionOP
there is no way to block access for all subfolders inside that folder?
adding them manually seems a bit difficult if I add more folders and pages
Answer
eg. create a route /admin