Koa

Install Kilpi for Koa


Install & setup Kilpi

Install Kilpi and setup your Kilpi instance by following the quickstart guide.

export const Kilpi = createKilpi({
getSubject,
policies,
...
})

1

Provide scope

To use the full feature set of Kilpi, you need to provide a scope for each request. This can be done by using the Kilpi.runInScope method as a middleware.

app.use(async (ctx, next) => {
Kilpi.runInScope(async () => {
await next();
})
})

2

Handle unauthorized errors

Since Koa supports throwing HTTP exceptions, handling errors is straightforward with a middleware.

app.use(async (ctx, next) => {
Kilpi.runInScope(async () => {
Kilpi.onUnauthorized(({ message }) => ctx.throw(403, message));
await next();
})
})

3