[Vuejs]-Check user permissions in vue js laravel

1👍

you need to rewrite your functions in JS and get the user details from the api, backend (PHP) and frontend (Vue.js) are completely independant.

An example :

1/ get logged user details, roles, permissions… with GET "/auth/me", store this in your app (vue store, local storage…)

2/ write a function to test if the user has a specific role

function hasRole(user, role) {
  if(!user || !user.role)
    return false

  if(user.role === 'super-admin')
    return true

  return role === user.role
}

You can do the same with permissions :

function hasPermission(user, permission) {
  if(!user || !user.permissions)
    return false

  if(user.role === 'super-admin')
    return true

  if(Array.isArray(permission))
    return user.permissions.some(i => permission.includes(i))
  else
    return user.permissions.includes(permission)
}

Leave a comment