Casbin giving errors of can't find 'fs'
I am having issues with casbin and its clearly from my lack of experience with using it.
I am using strapi v4, in my authProvider I get the users role and store it along with the other useful information.
I am now trying to use casbin to load a different dashboard based on who is logged in either admin or user but I keep getting an error that it can't find 'fs'. I have been following the tutorial on the documents page.
4 Replies
Hi @erekyu. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The error you're encountering is likely due to the fact that
casbin
requires certain Node.js modules like fs
which are not available in the browser environment. To resolve this, you need to configure polyfills for vite
to work in a browser environment.
First, install the required packages:
Then, add the following to your vite.config.ts
file:
This configuration will provide browser fallbacks for the Node.js modules that casbin
depends on. Without these overrides, casbin
versions >5
are known to throw errors (source (https://refine.dev/blog/refine-pixels-6/)).
Once you've done this, you should be able to use casbin
in your accessControlProvider
to enforce role-based access control. Here's an example of how you might do this:
In this example, the can
method fetches the user's role from the authProvider
and uses casbin
to enforce access control based on that role (source (https://refine.dev/blog/refine-pixels-6/)).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@66380739771379712> kapa.ai is still learning and improving, please let us know how it did by reacting below
blank-aquamarine•10mo ago
Hello @erekyu are you following this page? https://refine.dev/docs/advanced-tutorials/access-control/#adding-policy-and-model
If you can provide more information, we could help you. You can also provide a minimal-reproducible example.
Access Control | refine
Introduction
foreign-sapphire•10mo ago
the article the bot mentioned has the parts that are missing from the article you linked (the one you linked was the one I was following). It is really out dated and the github has the missing packages and other things but the you really gotta dig to find whats missing that makes the demo work. the other article has the missing packages and setup