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((req, res, next) => { Kilpi.runInScope(async () => { next(); });})
Handle unauthorized errors
To handle unauthorized errors in Express, we recommend the following approach with a global error handler after all route definitions.
import { KilpiError } from '@kilpi/core';
app.use((err, req, res, next) => { if (err instanceof KilpiError.AuthorizationDenied) { return res.status(403).json({ error: err.message }); }
// Handle other errors ...});