Skip to content
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

using with react (vite) #386

Closed
meodemsao opened this issue Sep 13, 2022 · 16 comments · Fixed by #440
Closed

using with react (vite) #386

meodemsao opened this issue Sep 13, 2022 · 16 comments · Fixed by #440
Assignees
Labels
question Further information is requested

Comments

@meodemsao
Copy link

meodemsao commented Sep 13, 2022

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

@casbin-bot
Copy link
Member

@casbin-bot casbin-bot added the question Further information is requested label Sep 13, 2022
@hsluoyz
Copy link
Member

hsluoyz commented Sep 13, 2022

@Shivansh-yadav13
Copy link
Member

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

@hsluoyz hsluoyz closed this as completed Sep 13, 2022
@nodece
Copy link
Member

nodece commented Sep 13, 2022

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

@Shivansh-yadav13
Copy link
Member

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

Ohk, I'll look into it.

@meodemsao
Copy link
Author

@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
Copy link
Author

@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

@Shivansh-yadav13
Copy link
Member

Shivansh-yadav13 commented Sep 15, 2022

@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?

@nodece
Copy link
Member

nodece commented Sep 15, 2022

@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 nodece reopened this Sep 15, 2022
@meodemsao
Copy link
Author

meodemsao commented Sep 17, 2022

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

@nodece
Copy link
Member

nodece commented Sep 17, 2022

Because I use the minimatch instead of the picomatch.

@meodemsao
Copy link
Author

Because I use the minimatch instead of the picomatch.

thanks u

@Shivansh-yadav13
Copy link
Member

Because I use the minimatch instead of the picomatch.

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

@nodece
Copy link
Member

nodece commented Sep 26, 2022

@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.

@ItsKDaniel
Copy link

ItsKDaniel commented Sep 27, 2022

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.

@hsluoyz
Copy link
Member

hsluoyz commented Jan 31, 2023

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
6 participants