williamcruzme / vue-gates

🔒 A Vue.js & Nuxt.js plugin that allows you to use roles and permissions in your components or DOM elements, also compatible as middleware and methods.
https://williamcruzme.github.io/vue-gates/
MIT License
263 stars 31 forks source link

Compatibility with vue-router #27

Open jcharcosset opened 4 years ago

jcharcosset commented 4 years ago

Is it planned an integration with vue-router ? Could we authorize route depend of roles and/or permissions with meta field

quantumwebco commented 4 years ago

Currently to integrate with Vue Router I'm putting the check in the beforeEnter callback

    {
        path: '/surveys/:survey_id/edit', component: SurveyForm,
        beforeEnter: (to, from, next) => {
            if (!router.app.$gates.hasAnyRole('Admin|Super Admin') || 
                !!router.app.$store.state.surveys.find(s => s.id === parseInt(to.params.survey_id)) {
                next('/surveys');
            }
            next();
        }
    },

It would be really nice to just pass meta: { middlewares: ['admin', 'owner'] } and if route params were available in the middleware