Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ We demonstrate the usage of Casbin.js with [a React app](https://github.com/casb


You can use `manual` mode in Casbin.js, and set the permission whenever you wish.

### Simple Permission Format
```javascript
const casbinjs = require('casbin.js');

Expand All @@ -44,6 +46,35 @@ authorizer.cannot("write", "data2").then(result => {
});
```

### Using CasbinJsGetPermissionForUser Format
If you're using a Go Casbin backend, you can directly use the output from `CasbinJsGetPermissionForUser`:

```javascript
const casbinjs = require('casbin.js');

// Permission from Go backend using CasbinJsGetPermissionForUser
// This format includes the model and policies
const permission = {
"m": "[request_definition]\nr = sub, obj, act\n...",
"p": [
["p", "alice", "data1", "read"],
["p", "alice", "data2", "write"]
],
"g": [
["g", "alice", "admin"]
]
}

const authorizer = new casbinjs.Authorizer("manual");

await authorizer.setPermission(permission);
await authorizer.setUser("alice");

// Evaluate the permission
const canRead = await authorizer.can("read", "data1");
console.log(canRead); // true
```

You can also use the `auto` mode. In details, specify a casbin backend service endpoint when initializing the Casbin.js authorizer, and set the subject when the frontend user identity changes. Casbin.js will automatically fetch the permission from the endpoint. (A pre-configurated casbin service API is required at the backend.)
```javascript
const casbinjs = require('casbin.js');
Expand Down
Loading
Loading