Install & setup Kilpi
Install Kilpi and setup your Kilpi instance by following the quickstart guide.
export const Kilpi = createKilpi({ getSubject, policies, ...})
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(); })})
Handle unauthorized errors
Since Oak 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(); })})