A convenient middleware for using corm-can with koa

Usage no npm install needed!

<script type="module">
  import cormCanKoa from '';



This is a simple module for generating koa-compatible middleware to add access control to particular routes using a user model loaded at this.user with the can() method attached. See corm-can for details on how to attach the can() method.


Once your user model is mounted and has the can() method available, you can simply do this;

const can = require('corm-can-koa')

// Make an app with some sort of chainable router that populates this.params.
const app = require('koa')()

// Load the user into this.user somehow

app.get('/users/:id', can('view', User, ':id'), function* (next) {
  // You'll only get here if you have the right permissions.
  // This will user the `id` value in this.params and the
  // User model to find and mount the record at this.model.
  // You can also do `?id` to search the query object instead.