question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

change AbilityBuilder on state update

See original GitHub issue

I have below code in ability.js

// Defines how to detect object's type
function subjectName(item) {
    if (!item || typeof item === 'string') {
        return item;
    }
    return item.__type;
}

let state = null;
store.subscribe(() => {state = store.getState()});

export default AbilityBuilder.define({subjectName}, can => {
    can('delete', 'Post', {userId: state.auth.userId});
});

and i use redux and react-redux for handling my state in react app

my question is, how can i update my AbilityBuilder when my app’s state updated? because my userId come from state.

thanks 😃

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
tanh123678commented, May 2, 2019

i do exactly the same to you but i cant update ability . please tell me why ??? when i logout and login to another user, the ability didn’t update.

4reactions
stalniycommented, Jan 11, 2019

Hi,

You don’t need to update AbilityBuilder. It’s just a helper class. What you want to do is to update rules in your Ability instance.

So, the approximate code should be something like this:

// Defines how to detect object's type
function subjectName(item) {
    if (!item || typeof item === 'string') {
        return item;
    }
    return item.__type;
}

const ability = new Ability([], { subjectName })

let currentAuth;
store.subscribe(() => {
   const prevAuth = currentAuth
   currentAuth = store.getState().auth

   if (prevAuth !== currentAuth) {
      ability.update(defineRulesFor(currentAuth))
   }
});

function defineRulesFor(auth) {
  const { can, rules } = AbilityBuilder.extract()

  can('delete', 'Post', { userId: auth.userId });

  return rules
}

export default ability
Read more comments on GitHub >

github_iconTop Results From Across the Web

Define Rules - CASL
There are 3 ways you can define abilities: using defineAbility function; using AbilityBuilder class; using JSON objects. In order to understand which way...
Read more >
stalniy-casl/casl - Gitter
Retrieve ids of all vehicles when you define rules. This works good in case if amount of vehicles not big (<= 1000). const...
Read more >
Casl Vue set ability on reload + in router + after login based on ...
The issue is that if you pass async function inside AbilityBuilder.define then AbilityBuilder.define returns a Promise .
Read more >
How to manage dynamic permissions using CASL & Redux
Here we are importing Ability and AbilityBuilder from @casl/ability . ... const authReducer = (state = INITIAL_STATE, action) => {. 4. switch (action.type) ......
Read more >
React, Manage Dynamic Permissions Using CASL & Redux.
import { Ability, AbilityBuilder } from "@casl/ability"; const ability ... const authReducer = (state = INITIAL_STATE, action) => { switch ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found