node-casbin icon indicating copy to clipboard operation
node-casbin copied to clipboard

using with react (vite)

Open meodemsao opened this issue 3 years ago • 12 comments

I have problem when using node-casbin with react

import { newModel, Enforcer, newEnforcer } from 'casbin'
import { AuthActionVerb, AuthPossession, CustomAuthActionVerb } from 'constants/enum/auth'
import { ExecutionContext } from 'graphql/execution/execute'

import storage from './storage'

export type Permission = {
  resource: string
  action: AuthActionVerb | CustomAuthActionVerb
  possession?: AuthPossession
}

export const loadModel = () => {
  // rbac model
  const model = `
	[request_definition]
	r = sub, obj, act

	[policy_definition]
	p = sub, obj, act

	[role_definition]
	g = _, _

	[policy_effect]
	e = some(where (p.eft == allow))

	[matchers]
	m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act`

  const m = newModel()
  m.loadModelFromText(model)
  return m
}

export const loadPermissions = async (enforcer: Enforcer) => {
  const permissions = storage.getPermissions()
  await Promise.all([
    ...permissions.map((permission: any) => {
      return enforcer.addPolicy(permission[0], permission[1], permission[2])
    }),
  ])
}

export const initEnforcer = async (): Promise<Enforcer | null> => {
  const m = loadModel()

  const enforcer = await newEnforcer(m)

  await loadPermissions(enforcer)

  return enforcer
}

Screen Shot 2022-09-13 at 4 18 16 PM

meodemsao avatar Sep 13 '22 09:09 meodemsao

@nodece @Zxilly @Shivansh-yadav13

casbin-bot avatar Sep 13 '22 09:09 casbin-bot

@meodemsao did you try: https://github.com/casbin-js/react-authz ?

hsluoyz avatar Sep 13 '22 12:09 hsluoyz

@meodemsao you can try ReactAuthz, Casbin-core, Casbin.js, node-casbin is not really for frontend javascript

Shivansh-yadav13 avatar Sep 13 '22 13:09 Shivansh-yadav13

This looks like a dependency issue, @Shivansh-yadav13 Could you work on this?

nodece avatar Sep 13 '22 15:09 nodece

This looks like a dependency issue, @Shivansh-yadav13 Could you work on this?

Ohk, I'll look into it.

Shivansh-yadav13 avatar Sep 13 '22 15:09 Shivansh-yadav13

@meodemsao did you try: https://github.com/casbin-js/react-authz ?

i don't know how to use this, don't have any doc

meodemsao avatar Sep 13 '22 17:09 meodemsao

@meodemsao you can try ReactAuthz, Casbin-core, Casbin.js, node-casbin is not really for frontend javascript

i will try https://github.com/casbin/casbin-core

node-casbin work if i use with webpack, i have issue with vite

meodemsao avatar Sep 13 '22 17:09 meodemsao

@nodece @meodemsao I feel the code is running in browser, that's why we are getting this error, do you think there could be a bug or dependency error?

Shivansh-yadav13 avatar Sep 15 '22 12:09 Shivansh-yadav13

@nodece @meodemsao I feel the code is running in browser, that's why we are getting this error, do you think there could be a bug or dependency error?

Looks like a dependency error, could you testing the the https://vitejs.dev/guide/ ?

nodece avatar Sep 15 '22 14:09 nodece

@Shivansh-yadav13 @nodece i used casbin-core and work well

meodemsao avatar Sep 17 '22 15:09 meodemsao

Because I use the minimatch instead of the picomatch.

nodece avatar Sep 17 '22 15:09 nodece

Because I use the minimatch instead of the picomatch.

thanks u

meodemsao avatar Sep 18 '22 04:09 meodemsao

Because I use the minimatch instead of the picomatch.

@nodece should we do that in node-casbin also?

Shivansh-yadav13 avatar Sep 26 '22 08:09 Shivansh-yadav13

@Shivansh-yadav13 I think we should use the minimatch instead of the picomatch, but some expressions cannot run on the minimatch, I checked the expressions, and I can accept that these expressions don't work.

nodece avatar Sep 26 '22 14:09 nodece

This does not work with Vue + Vite as well. I am getting the same error when I tried using vue-authz, or casbin.js.

The issue is due to polyfills for Node not being available in the main casbin npm package (node-casbin). Since casbin.js and the wrappers like react-authz, vue-authz are browser targeting libraries, we would require node polyfills to be available for the libraries to work.

ItsKDaniel avatar Sep 27 '22 15:09 ItsKDaniel

@imp2002 @Shivansh-yadav13 @nodece

hsluoyz avatar Sep 27 '22 16:09 hsluoyz

@ItsKDaniel casbin.js and the wrappers like react-authz are for browser only. It's not for Node.js . If you have other questions, please create new issue. Closed here.

hsluoyz avatar Jan 31 '23 13:01 hsluoyz