Install and setup Kilpi
Install Kilpi and setup your Kilpi instance by following the quickstart guide.
export const Kilpi = createKilpi({ ... }) Handle unauthorized errors from .assert()
The .assert() method throws an error when authorization is denied.
In Express, the easiest way to handle these errors is to allow Kilpi to throw the default KilpiError.Unauthorized error and handle it in a global error handler.
import { KilpiError } from "@kilpi/core";
// Global error handlerapp.use((err, req, res, next) => { if (err instanceof KilpiError.Unauthorized) { // Optionally customize behavior based on the decision switch (err.decision.reason) { ... }
// By default, send a HTTP 403 Forbidden response return res.status(403).json({ error: err.decision.message }); }
// Handle other errors ...});