Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@casl/ability

Package Overview
Dependencies
Maintainers
0
Versions
85
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@casl/ability

CASL is an isomorphic authorization JavaScript library which restricts what resources a given user is allowed to access

  • 6.7.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
647K
decreased by-0.43%
Maintainers
0
Weekly downloads
 
Created

What is @casl/ability?

@casl/ability is a powerful and flexible library for managing access control in JavaScript applications. It allows you to define what various users can or cannot do in your application, making it easier to implement role-based access control (RBAC) and other permission systems.

What are @casl/ability's main functionalities?

Define Abilities

This feature allows you to define what actions a user can or cannot perform on specific resources. In this example, the user can read articles but cannot delete them.

const { AbilityBuilder, Ability } = require('@casl/ability');

const { can, cannot, build } = new AbilityBuilder(Ability);

can('read', 'Article');
cannot('delete', 'Article');

const ability = build();

console.log(ability.can('read', 'Article')); // true
console.log(ability.can('delete', 'Article')); // false

Check Abilities

This feature allows you to check if a user has the ability to perform a specific action on a resource. The example shows how to check if a user can read or delete an article.

const { Ability } = require('@casl/ability');

const ability = new Ability([
  { action: 'read', subject: 'Article' },
  { action: 'delete', subject: 'Article', inverted: true }
]);

console.log(ability.can('read', 'Article')); // true
console.log(ability.can('delete', 'Article')); // false

Define Abilities with Conditions

This feature allows you to define abilities with specific conditions. In this example, the user can read articles only if they are published.

const { AbilityBuilder, Ability } = require('@casl/ability');

const { can, build } = new AbilityBuilder(Ability);

can('read', 'Article', { published: true });

const ability = build();

console.log(ability.can('read', { published: true })); // true
console.log(ability.can('read', { published: false })); // false

Integrate with Frontend Frameworks

This feature allows you to integrate @casl/ability with frontend frameworks like React. The example shows how to conditionally render a component based on the user's abilities.

import { Ability } from '@casl/ability';
import { Can } from '@casl/react';

const ability = new Ability([
  { action: 'read', subject: 'Article' }
]);

function App() {
  return (
    <Can I="read" a="Article" ability={ability}>
      <p>You can read this article!</p>
    </Can>
  );
}

export default App;

Other packages similar to @casl/ability

Keywords

FAQs

Package last updated on 30 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc