can-it.github.io

@can-it/nest

The @can-it/nest is a Nest (Nestjs) library that provides an easy and efficient way to implement authorization in your application. With this package, you can quickly integrate complex authorization scenarios into your app in just a few minutes. npm version

Examples

👉 Source code.

Installation

To install this package, you can use npm, yarn, or pnpm. Run one of the following commands in your project directory:

npm install @can-it/nest
yarn add @can-it/nest
pnpm add @can-it/nest

Usage

👉 Important: You should configure the RI (Resource Identity) resolver at the module scope using the CanItModule.configure method to avoid duplicating logic in your application whenever possible.

    // ...

    @Get(':id')
    @AllowTo('view')
    // The use of @UseRiResolver below will return "cats" as the resource identity
    // The returned value will be combined with the "view" action provided from @AllowTo above
    @UseRiResolver(
      (context: ExecutionContext, thisModule: ModuleRef) => {
        return 'cats';
      }
    )
    getOne(@Param('id') id: string) {
      // your handler logic here
    }

    // ...