afraid-scarlet
afraid-scarlet11mo ago

accessControlProvider is set first but login is done afterwards so I get role afterwards

accessControlProvider is set first but login is done afterwards so I get role afterwards
4 Replies
kapa.ai
kapa.ai11mo ago
Hi @saarthak08. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In refine, the accessControlProvider is indeed set up before the login process. However, the accessControlProvider's can method, which is used to control access, is called asynchronously. This means it doesn't execute immediately when the app loads, but rather when an access control check is needed. In the login process, you would typically store the user's role in the local storage or another similar place. Then, in the can method of the accessControlProvider, you would retrieve the role from where you stored it and use it to determine access. Here's an example from the extracts:
accessControlProvider={{
can: async ({ resource, action }) => {
let can: boolean = false;
const stringifyUser = localStorage.getItem('refine-user');
if (stringifyUser) {
const { roles } = JSON.parse(stringifyUser);

roles.forEach((role: string) => {
switch (action) {
case 'list':
case 'show':
can = ac.can(role).read(resource).granted;
break;
case 'create':
can = ac.can(role).create(resource).granted;
break;
case 'edit':
can = ac.can(role).update(resource).granted;
break;
case 'delete':
can = ac.can(role).delete(resource).granted;
break;
}
});
}
return Promise.resolve({ can });
},
}}
accessControlProvider={{
can: async ({ resource, action }) => {
let can: boolean = false;
const stringifyUser = localStorage.getItem('refine-user');
if (stringifyUser) {
const { roles } = JSON.parse(stringifyUser);

roles.forEach((role: string) => {
switch (action) {
case 'list':
case 'show':
can = ac.can(role).read(resource).granted;
break;
case 'create':
can = ac.can(role).create(resource).granted;
break;
case 'edit':
can = ac.can(role).update(resource).granted;
break;
case 'delete':
can = ac.can(role).delete(resource).granted;
break;
}
});
}
return Promise.resolve({ can });
},
}}
In this example, the user's role is retrieved from local storage and used to determine access. This way, even though the accessControlProvider is set up before login, it can still use the role that is obtained after login. For more details, you can refer to the Access Control Provider documentation (https://refine.dev/docs/api-reference/core/providers/accessControl-provider/) and the Access Control tutorial (https://refine.dev/docs/advanced-tutorials/access-control/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai11mo ago
<@783220602336444416> kapa.ai is still learning and improving, please let us know how it did by reacting below
fair-rose
fair-rose11mo ago
Hello @saarthak08 what is your problem exactly?
afraid-scarlet
afraid-scarlet11mo ago
Hey @batuhanw thanks for replying. It is resolved now