Update README.md
1 file changed
tree: 5284f757248aefc822a12c08effa5ff892e98466
  1. src/
  2. .gitignore
  3. jest.config.js
  4. package.json
  5. README.md
  6. tsconfig.json
README.md

Casbin.js Server Utilities for Node-Casbin

If you are using Casbin.js at your frontend and Node-Casbin as your backend Casbin service, you can install this package at your backend. This package provides a wrapper for generating the user permission, which can be passed to Casbin.js at the frontend.

Installation

npm install --save @casbinjs/server-utils
# or
yarn add @casbinjs/server-utils

Example

import {getUserPermission} from '@casbinjs/server-utils'

// In your Restful API
private async setRouter(): Promise<void> {
    this.app.get('/api/casbin', async (req: express.Request, res: express.Response) => {
        // Get the user identity from URL.
        const user = String(req.query["casbin_user"]);
        // Generate the valid permission string. You need to inialize a Casbin enforcer before calling the `getUserPermission` 
        const permission = await getUserPermission(enforcer, user);
        res.status(200).json({
            message: 'ok',
            data: permission
        })
    })
}