add .npmignore, genJsonProfile return string instead of object.
4 files changed
tree: 497c52c3bccd3e0dc3c3cdae67c8998e75c31883
  1. src/
  2. .eslintignore
  3. .eslintrc.js
  4. .gitignore
  5. .npmignore
  6. jest.config.js
  7. package.json
  8. README.md
  9. 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 profile passed to Casbin.js at the frontend.

Installation

npm install --save casbinjs-server-tool
# or
yarn add casbinjs-server-tool

Example

import CasbinServerTool from 'casbinjs-server-tool'

// 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"]);

        // Initialize the casbin server tool with casbin enforcer
        const casbinSvrTool = new CasbinServerTool(enforcer);
        
        // Generate the user's profile
        const profile = casbinSvrTool.genJsonProfile(user);

        // Return the response to the client-side.
        res.status(200).json({
            message: 'ok',
            data: profile
        })
    })
}