stalniy / casl

CASL is an isomorphic authorization JavaScript library which restricts what resources a given user is allowed to access
https://casl.js.org/
MIT License
5.74k stars 257 forks source link

Use ability created by express in Vue3 #908

Closed vahidsabet closed 2 months ago

vahidsabet commented 2 months ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like A clear and concise description of what you want to happen.

I defined ability as following:

const { AbilityBuilder, createMongoAbility  } = require('@casl/ability');

module.exports = (user) => {

const { can, cannot, build } = new AbilityBuilder(createMongoAbility)

if (user) {

switch (user.role) {
     case 'superadm':
         can('manage', 'all');
     break;
     break;
    default:
        can(['read'], ['user']);
     break;
  }
}

return build()

}

In the nodejs and express it's ok but when I use it after API call in Vue3 and user login, it won't work. Login method contains:

const { accessToken, userData, userAbilityRules } = res
useCookie('userAbilityRules').value = userAbilityRules
ability.update(userAbilityRules)

Describe alternatives you've considered (optional) A clear and concise description of any alternative solutions or features you've considered.

I've used

ability.update([ // switch to readonly mode
 { action: 'read', subject: 'all' }
])

and it works. But I want get the rules from backend.

Additional context (optional) Add any other context or screenshots about the feature request here.