-
-
Notifications
You must be signed in to change notification settings - Fork 276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
change AbilityBuilder on state update #148
Comments
Hi, You don't need to update 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 |
@ahmadiehsan does this help you? |
Close due to inactivity |
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. |
@tanh123678 please check that you use the latest version of casl/ability and casl/react packages |
Also please check this comment #174 (comment) The guy had the similar issue and in PR to his example I explained where he used CASL and react incorrectly. |
Hi guys I'm having the same problem that @tanh123678 is having: logout and login as different user won't have ability settings updated. The app is written in vue and using @casl/vue. Is there a way for me to empty all ability at logout? Why is Thanks. |
You need to update ability on login and logout That’s why your abilities are not updated |
Thanks @stalniy I'm running |
You shouldn’t do this :) This line should be written only once. Somewhere in Please check this answer https://stackoverflow.com/questions/53958961/update-user-in-casl and example with login flow via Vuex (https://github.com/stalniy/casl-vue-api-example) |
Hi @stalniy thanks for the guidance. Currently my code does the following:
Also in
And in
I can see defineRulesFor is run with correct rules returned. But when I try to get |
you create ability inside Please pay attention to how |
Thanks @stalniy; totally forgot about the scope of access for ability. I've reworked my code to the follow for anyone else stumble upon this issue: main.js:
imports/ability.js:
login and logout methods:
|
I have below code in ability.js
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 :)
The text was updated successfully, but these errors were encountered: