how to use dynamic routes in middleware.js #5010
Replies: 3 comments 9 replies
-
The matcher follow the same logic as the routes in Next.js. In your case. If you want to protect both "/product" route and the "/product/[productId]" route you would write your middleware this way:
|
Beta Was this translation helpful? Give feedback.
-
You have the possibility to match like this for this path:
|
Beta Was this translation helpful? Give feedback.
-
You can also you custom middleware and authenticate dynamic routes as const dynamicRouteRegex = /^/products/[^\/]+$/; |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I was looking in the NextAuth documentation to protect the pages using the middleware.js file, and I found the code snippet from NextAuth's Documentation.
here, I have a question, as in
matcher
it passes the regular page (file) name, but how to protect the dynamic pages? e.g./pages/[productId]/index.js
or/pages/product/[index].js
, how to protect those kinds of pages using themiddleware.js
file?also, as per the example shared above, if I hit the
URL/dashboard
page directly then it'll redirect to the Login page, then after successful Login, does it'll redirect back to the Dashboard Page?also, if I use a Custom Login Page instead of NextAuth's default Login Page, then this
middleware
will work the same way?please explain/help me here.
Beta Was this translation helpful? Give feedback.
All reactions