# allowjs

> A lightweight permissioning library.

Latest version **1.1.4** (published 2026-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install allowjs
pnpm add allowjs
yarn add allowjs
bun add allowjs
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.1.4 |
| Published | 2026-03-11 |
| First published | 2020-12-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18.18.0 |
| Dependencies | 0 |
| Unpacked size | 17.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 5 |
| Maintainers | petermyers |
| Keywords | allow, permissions, access, control, authorize, authorization |

## Links

- npm: https://www.npmjs.com/package/allowjs
- Repository: https://github.com/petermyers/allow
- Homepage: https://github.com/petermyers/allow#readme
- Issues: https://github.com/petermyers/allow/issues
- npm.io page: https://npm.io/package/allowjs

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 1.1.4 (latest) — 2026-03-11
- 1.1.3 — 2026-03-11
- 1.1.2 — 2026-03-11
- 1.1.1 — 2024-07-11
- 1.1.0 — 2024-07-11
- 1.0.3 — 2020-12-19
- 1.0.2 — 2020-12-19
- 1.0.1 — 2020-12-19
- 1.0.0 — 2020-12-19
- 0.1.5 — 2020-12-19
- 0.1.4 — 2020-12-19
- 0.1.3 — 2020-12-19
- 0.1.1 — 2020-12-18
- 0.1.0 — 2020-12-18
- 0.0.6 — 2020-12-18
- … 4 more at https://npm.io/package/allowjs/versions

## README

<h1 align="center">Welcome to Allow 👋</h1>
<p>
  <img src="https://img.shields.io/badge/node-%3E%3D10.0.0-blue.svg" />
  <a href="https://github.com/petermyers/allow#readme" target="_blank">
    <img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" />
  </a>
  <a href="https://codecov.io/gh/petermyers/allow/">
    <img alt="Coverage" src="https://img.shields.io/codecov/c/github/petermyers/allow/main.svg?style=flat-square" />
  </a>
  <a href="https://github.com/petermyers/allow/graphs/commit-activity" target="_blank">
    <img alt="Maintenance" src="https://img.shields.io/badge/Maintained%3F-yes-brightgreen.svg" />
  </a>
  <a href="https://github.com/petermyers/allow/blob/master/LICENSE" target="_blank">
    <img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-brightgreen.svg" />
  </a>
</p>

> A lightweight permissioning library.

### 🏠 [Homepage](https://github.com/petermyers/allow#readme)

## Prerequisites

- node >=18.18.0

## Installation
```sh
npm install allowjs
```
or
```sh
yarn add allowjs
```
## Usage
Imports
```ts
import Ability, { Permissions, AbilityInterface, NotAuthorizedError } from 'allowjs';
```

### Simple Allowance
```ts
const userAbility = new Ability<User>(abilityInterface => {
  const { allow } = abilityInterface;

  allow(Permissions.READ, SomeClass, (user: User, entity: SomeClass) => user.id === entity.userId);
});
```

### Disallowing
```ts
const userAbility = new Ability<User>(abilityInterface => {
  const { allow, disallow } = abilityInterface;

  allow(Permissions.READ, SomeClass, (user: User, entity: SomeClass) => user.id === entity.userId);

  disallow(Permissions.READ, SomeClass, (user: User, entity: SomeClass) => user.roles.includes("role"));
});
```

Permissions are granted based on the following conditions:
- At least one `allow` criteria is satisfied the by provided user.
- No `disallow` criteria are satisfied by the provided user.

### Checking Permissions
You can check permissions by using `permits` and `ensures` functions on the ability.
- `permits` - Will return a boolean, whether or not the user has the specified permissions.
- `ensure` - Will throw a `NotAuthorizedError` if the user does not have the required permissions. This can be useful in frameworks like Express where you can specify an error handler for this type of exception and return a 401 status code.

```ts
if(userAbility.permits(user).toPerform(Permissions.READ).on(someEntity)) {
  // Take some action
}
```
```ts
// Throws a NotAuthorizedError if user cannot perform READ on someEntity
userAbility.ensure(user).canPerform(Permissions.READ).on(someEntity)
```

### Class Level Permissions
You can also specify that some particular user has permissions broadly across some class type.
```ts
const userAbility = new Ability<User>(abilityInterface => {
  const { allow, disallow } = abilityInterface;

  allow(Permissions.READ, SomeClass, (user: User) => user.roles.includes('role'));
});
```

and check them by passing in the class type instead of an instance during the permissions check
```ts
if(userAbility.permits(user).toPerform(Permissions.READ).on(SomeClass)) {
  // Take some action
}
```
or
```ts
// Throws a NotAuthorizedError if user cannot perform READ on SomeClass
userAbility.ensure(user).canPerform(Permissions.READ).on(SomeClass)
```

### Default Permissions
Out of the box the following permissions are provided:
```
Permissions.CREATE
Permissions.READ
Permissions.UPDATE
Permissions.DELETE
```

### CRUD Expansion
`Permissions.CRUD` is provided as a shorthand for specifying all of `Permissions.CREATE`, `Permissions.READ`, `Permissions.UPDATE`, and `Permissions.DELETE`.
```ts
allow(Permissions.CRUD, SomeClass, (user: User, entity: SomeClass) => entity.userId === user.id);

// is equivalent to
allow(Permissions.CREATE, SomeClass, (user: User, entity: SomeClass) => entity.userId === user.id);
allow(Permissions.READ, SomeClass, (user: User, entity: SomeClass) => entity.userId === user.id);
allow(Permissions.UPDATE, SomeClass, (user: User, entity: SomeClass) => entity.userId === user.id);
allow(Permissions.DELETE, SomeClass, (user: User, entity: SomeClass) => entity.userId === user.id);
```

### Custom Permissions
You can provide permissions in the form of a string if the standard `CRUD` operations are insufficent.
```ts
userAbility.allow('permission.custom', SomeClass, (user: User, entity: SomeClass) => entity.userId === user.id);

userAbility.permits(user).toPerform('permission.custom').on(someEntity);
```

### Freeform Entity Types
You can also specify a custom entity type that isn't restricted to a particular class as a name string. When checking the permissions against the object, you will need to specify the type you want to treat it as.

```ts
userAbility.allow(Permission.READ, 'type.custom', (user: User, entity: { userId: string }) => entity.userId === user.id);

userAbility.permits(user).toPerform(Permission.READ).on(someObject, 'type.custom');
```
If you don't mind it, you could also just `entity: any` in plcae of the `entity` type definition.

## Development
## Install
```sh
yarn install
```

## Tests
```sh
yarn test
yarn prettier:check
yarn lint:check
```

## Author

👤 **Peter Myers**

* Github: [@petermyers](https://github.com/petermyers)

## 🤝 Contributing

Contributions, issues and feature requests are welcome!<br />Feel free to check [issues page](https://github.com/petermyers/allow/issues).

## Show your support

Give a ⭐️ if this project helped you!

## 📝 License

Copyright © 2020 [Peter Myers](https://github.com/petermyers).<br />
This project is [MIT](https://github.com/petermyers/allow/blob/master/LICENSE) licensed.

***
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_

---
_Source: https://npm.io/package/allowjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
