JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 28
  • Score
    100M100P100Q42228F
  • License MIT

Minimal authorization through a plain old JavaScript object

Package Exports

  • pundit

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pundit) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

pundit Build Status js-standard-style

Minimal authorization through a plain old JavaScript object, ported to JS from the Ruby gem. Fits nicely into any app/framework including Express, Micro, React, Vue, Graphql, etc.

Installation

npm install --save pundit

Usage

In order to use Pundit, you can initialize a policy by passing an object of functions. These functions are called actions. Actions typically map to routes in your application.

A policy accepts a user, often the current user of your session, and the resource you wish to authorize against.

const pundit = require('pundit')

const PostPolicy = pundit({
  edit: (user, record) => user.id === record.id,
  destroy: (user) => user.isAdmin()
})

const policy = new PostPolicy(user, record)

policy.edit()
policy.destroy()

Using with Express

Using with Graphql

In Graphql land, you can authorize returned objects using Pundit in the resolve callback of the query.

Using with React

You can also expose UI elements when the current user is authorized using the When component. It is recommended to wrap When with your own Component that automatically defines the user and policy for the resource.

<When
  user={currentUser}
  can='edit'
  resource={post}
  policy={policy}
>
  <EditButton />
</When>

License

MIT

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Crafted with <3 by John Otander (@4lpine).


This package was initially generated with yeoman and the p generator.