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.
import { Request, Response, NextFunction } from 'express';
function kilpiMiddleware(req: Request, res: Response, next: NextFunction) { Kilpi.runInScope(async () => { next(); })};
app.use(kilpiMiddleware)
Handle unauthorized errors
Since Nest.js supports throwing HTTP exceptions, handling errors is straightforward with a middleware.
function kilpiMiddleware(req: Request, res: Response, next: NextFunction) { Kilpi.runInScope(async () => { Kilpi.onUnauthorized(() => { throw new ForbiddenException(); })
next(); })};