How to send some parameter to middleware?
For example, I have two pages: all_pages
and all_users
.
They initializaded with middleware. In the middleware, you need to check if the user object has permission. The problem is that for different pages you need to check different permissions. And you will have to create many similar types of middleware. It would be wonderful to have the ability to transfer to the middleware the resolution that should be checked:
// pages/all_pages.veu
export default {
name: 'all_pages',
middleware: ['check_access?permission=edit_pages'],
}
```javascript
// pages/all_users.veu
export default {
name: 'all_users',
middleware: ['check_access?permission=edit_users'],
}
```javascript
// middleware/check_access.js
export default function ({ store, error, params }) {
if (!store.state.currentUser.permissions.find(p => p === params.permission) {
return error(/* ... */)
}
}
It's really useful example when we can use meta variables for routes. Unfortunately, meta fields not supported by Nuxt.
In my project, I also have some scenarios when meta fields excellent solutions because some logic depends exactly on the page, but not the state. That's why vuex not so good solutions in this case.
For example. All pages have menu component, that why this component placed at the layout, but not every page. Now I want to make this menu a little different at a couple of pages. It's really easy with meta variables and really overhead with new custom layouts.
@pi0, @Atinux, guys, what do you think about it?
p.s. Sorry for my English.
Hi @cawa-93 @KonstantinVlasov
You can already check some params from the matched route directly, which is even better than meta or middleware params.
See my demonstration: https://glitch.com/edit/#!/nuxt-midd-properties?
Example: https://nuxt-midd-properties.glitch.me/
You pages could look like this:
// pages/all_pages.vue
export default {
middleware: ['check_access'],
permissions: ['edit_pages']
}
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Hi @cawa-93 @KonstantinVlasov
You can already check some params from the matched route directly, which is even better than meta or middleware params.
See my demonstration: https://glitch.com/edit/#!/nuxt-midd-properties?
Example: https://nuxt-midd-properties.glitch.me/
You pages could look like this: