genetic-orange
genetic-orange9mo ago

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
kapa.ai
kapa.ai9mo ago
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:
npm install -D rollup-plugin-polyfill-node @esbuild-plugins/node-modules-polyfill @esbuild-plugins/node-globals-polyfill
npm install -D rollup-plugin-polyfill-node @esbuild-plugins/node-modules-polyfill @esbuild-plugins/node-globals-polyfill
Then, add the following to your vite.config.ts file:
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";
import rollupNodePolyFill from "rollup-plugin-polyfill-node";

export default defineConfig({
plugins: [react()],
optimizeDeps: {
esbuildOptions: {
define: {
global: "globalThis",
},
plugins: [
NodeGlobalsPolyfillPlugin({
buffer: true,
process: true,
}),
NodeModulesPolyfillPlugin(),
],
},
},
build: {
rollupOptions: {
plugins: [rollupNodePolyFill()],
},
},
});
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";
import rollupNodePolyFill from "rollup-plugin-polyfill-node";

export default defineConfig({
plugins: [react()],
optimizeDeps: {
esbuildOptions: {
define: {
global: "globalThis",
},
plugins: [
NodeGlobalsPolyfillPlugin({
buffer: true,
process: true,
}),
NodeModulesPolyfillPlugin(),
],
},
},
build: {
rollupOptions: {
plugins: [rollupNodePolyFill()],
},
},
});
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:
import { newEnforcer } from "casbin";
import { CanParams, CanReturnType } from "@refinedev/core";
import { adapter, model } from "../casbin/accessControl";
import { authProvider } from "./authProvider";
export const accessControlProvider = {
can: async ({ resource, action }: CanParams): Promise<CanReturnType> => {
const role = await authProvider.getPermissions?.();
const enforcer = await newEnforcer(model, adapter);
const can = await enforcer.enforce(role, resource, action);
return Promise.resolve({
can,
});
},
};
import { newEnforcer } from "casbin";
import { CanParams, CanReturnType } from "@refinedev/core";
import { adapter, model } from "../casbin/accessControl";
import { authProvider } from "./authProvider";
export const accessControlProvider = {
can: async ({ resource, action }: CanParams): Promise<CanReturnType> => {
const role = await authProvider.getPermissions?.();
const enforcer = await newEnforcer(model, adapter);
const can = await enforcer.enforce(role, resource, action);
return Promise.resolve({
can,
});
},
};
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: ----------------------
kapa.ai
kapa.ai9mo ago
<@66380739771379712> kapa.ai is still learning and improving, please let us know how it did by reacting below
stormy-gold
stormy-gold9mo 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.
genetic-orange
genetic-orange9mo 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