can-it.github.io

@can-it/core

@can-it/core provides functionality for handling access control and permission checks based on actions and resource identification. npm version

Examples

👉 Source code.

Installation

You can install the library using npm. Run the following command in your project directory:

npm install @can-it/core

Concepts

Usage

Using this package is easy. Follow the example below to get started:

// Import classes from the library
import { CanIt } from '@can-it/core';
import { ExactComparator } from '@can-it/operators-exact';

// Create a CanIt instance by providing:
// - Policy
// - Action Comparator
// - Ri Comparator
const canIt = new CanIt(
  {
    allow: [
      ['create', 'posts'],
      ['delete', 'posts']
    ],
    deny: [
      ['delete', 'users']
    ]
  },
  new ExactComparator(), // You can leave it as undefined; it will use the ExactComparator as the default.
  new ExactComparator() // You can leave it as undefined; it will use the ExactComparator as the default.
);

// Check whether a request is allowed or not
console.log(
  canIt.allowTo('create', 'posts')
);
// Output: true

console.log(
  canIt.allowTo('delete', 'users')
);
// Output: false

License

This project is licensed under the MIT License.