Issues with Nested routing
Hello,
I am having hard time with nested routing.
I have roles, guest, user and admin roles, and want to restrict guest from accessing user and admin routes, once logged in restrict user from accessing admin's routes and vice versa.
I have added middleware to index.vue inside the folder but it only guards index file. I am looking for middleware guard all the files in the folder.
here is the structure of the folder
--pages
|--index.vue
|--Investor // Parent Folder
|--index.vue // Parent Main page
|--overview.vue // Child page
|--profile.vue // Child page
2 Replies
Hi @Slater8242. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
I understand you're having issues with nested routing and middleware in Nuxt, specifically with protecting routes based on user roles.
The issue you're facing is that middleware applied to the parent
index.vue
file doesn't automatically apply to all child routes in the folder. Let's solve this problem.
Solution: Global Middleware for Role-Based Access
You have a few options to protect all routes in a folder:
Option 1: Create a global middleware
Create a global middleware file that checks user roles and restricts access accordingly:
Global middleware runs on every route change, so this will protect all your routes Docs > Guide > Directory Structure > Middleware.
Option 2: Apply middleware to each page
You can add the middleware to each page using definePageMeta
:
However, this would be repetitive as you'd need to add it to every page.
Option 3: Set middleware at build time
A more elegant solution is to use the pages:extend
hook in your nuxt.config.ts
to apply middleware to all pages in specific directories:
This approach automatically applies the specified middleware to all pages in the Investor directory, including nested routes Docs > Guide > Directory Structure > Middleware.
Creating the role-based middleware
Then create your middleware file:
This approach ensures that all routes within the Investor directory are protected, regardless of how deeply nested they are.
Remember that middleware runs in a specific order: global middleware first, then page-defined middleware Docs > Guide > Directory Structure > Middleware.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@558009944012619800> Kapa.ai is still learning and improving, please let me know how I did by reacting below.