Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
How to make two permission menu with 2 role login in Vue JS 3

My router file checking what role is logged in:

router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.requiresAdmin)) {
        if(VueJwtDecode.decode(localStorage.getItem('accessToken')).sub == "admin"){
            next()
            return
        }
        next('/auth/login')
    }else if (to.matched.some(record => record.meta.requiresStaff)) {
        if(VueJwtDecode.decode(localStorage.getItem('accessToken')).sub == "staff"){
            next()
            return
        }
        next('/auth/login')
    }else {
        next()
    }
})

and my admin router is:

const adminRoutes = [
    {
        path: '/dashboard',
        name: 'admin.dashboard.index',
        component: () => import( /* webpackChunkName: "admin.dashboard.index" */ '@/views/administrator/dashboard/Index.vue'),
        meta: { requiresAdmin: true, requiresStaff: true, layout: 'default' }, /* Look At this Line */
    },
    // Article Route
    {
        path: '/article',
        name: 'admin.article.index',
        component: () => import( /* webpackChunkName: "admin.article.index" */ '@/views/administrator/article/Index.vue'),
        meta: { requiresAdmin: true, layout: 'default' },
    },
]

I have already add requiresAdmin: true, requiresStaff: true, in meta field. But when i login with admin, its work. But when i login with staff account, i cant reach dashboard page.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try can try with one role for both:

else if (to.matched.some(record => record.meta.requiresAdminStaff)) {
    if(VueJwtDecode.decode(localStorage.getItem('accessToken')).sub == "staff" || 
       VueJwtDecode.decode(localStorage.getItem('accessToken')).sub == "admin"){
        next()
        return
    }
    next('/auth/login')

then in admin routes :

{
    path: '/dashboard',
    name: 'admin.dashboard.index',
    component: () => import( /* webpackChunkName: "admin.dashboard.index" */ '@/views/administrator/dashboard/Index.vue'),
    meta: { requiresAdminStuff: true, layout: 'default' }, /* Look At this Line */
},
about 4 years ago · Juan Pablo Isaza Relatório

0

The problem is that when you are logged in as staff and you access the dashboard page, this condition is still true: if (to.matched.some(record => record.meta.requiresAdmin)). But as you have the permission staff you get redirected to the login page.

A solution would be to define the allowed roles per page explicitly.

const adminRoutes = [
    {
        path: '/dashboard',
        name: 'admin.dashboard.index',
        component: () => import( /* webpackChunkName: "admin.dashboard.index" */ '@/views/administrator/dashboard/Index.vue'),
        meta: { roles: ['admin', 'staff'], layout: 'default' },
    },
    // Article Route
    {
        path: '/article',
        name: 'admin.article.index',
        component: () => import( /* webpackChunkName: "admin.article.index" */ '@/views/administrator/article/Index.vue'),
        meta: { roles: ['admin'], layout: 'default' },
    },
]
router.beforeEach((to, from, next) => {
    const currentRole = VueJwtDecode.decode(localStorage.getItem('accessToken')).sub
    const hasPublicAccess = !record.meta.roles || record.meta.roles.length === 0

    if (hasPublicAccess || to.matched.some(record => record.meta.roles.includes(currentRole)) {
        next()
        return
    }

    next('/auth/login')
    return
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda