Usage on client

Learn how to use Kilpi on the client side with @kilpi/client.


You can also use Kilpi on the client side with the @kilpi/client package which allows you to fetch authorization decisions from the server.


Setup

Install the @kilpi/client package

Setup environment variables

Create public environment variables (e.g. with PUBLIC_, NEXT_PUBLIC_ or similar prefix to ensure they are included in the client bundle).

Terminal window
PUBLIC_KILPI_URL=http://localhost:3000/api/kilpi
PUBLIC_KILPI_SECRET=generate-secret

Install EndpointPlugin

The Kilpi client requires an endpoint for fetching authorization decisions. Use the EndpointPlugin from @kilpi/core to create an authorization endpoint. It exposes the Kilpi.$createPostEndpoint() function, which constructs a web-standard request-response function to use as your endpoint.

kilpi.ts
import { createKilpi, EndpointPlugin } from "@kilpi/core";
export const Kilpi = createKilpi({
...,
plugins: [
EndpointPlugin({
secret: process.env.PUBLIC_KILPI_SECRET,
// Optional
getContext(request) { ... },
async onBeforeHandleRequest(request) { ... },
async onBeforeProcessItem(requestItem) { ... },
})
]
})

Read more about the EndpointPlugin for more information on the setup options.

Setup the authorization endpoint

Expose the endpoint using your framework of choice.

Create your KilpiClient instance

Create your KilpiClient instance with createKilpiClient. This object is used to fetch authorization decisions from the server.

Optionally, pass infer for improved type safety, and add any plugins via plugins.

kilpi-client.ts
import type { Kilpi } from "./kilpi.ts";
export const KilpiClient = createKilpiClient({
// Infer subject and policies from server instance
infer: {} as typeof Kilpi,
// Connect to the endpoint
connect: {
endpointUrl: process.env.PUBLIC_KILPI_URL,
secret: process.env.PUBLIC_KILPI_SECRET,
},
});

Fetch authorization decisions on the client

Now you can use KilpiClient to fetch authorization decisions from the server with a similar API as on the server.

KilpiClient automatically provides request caching, batching and deduplication for performance.

// Similar decision object as on the server
const decision = await KilpiClient.posts.edit(myPost).authorize();

Use with your frontend framework

You can use @kilpi/client with your frontend framework of choice or use one of the provided plugins that provide e.g. components and other useful bindings for KilpiClient.


The authorize() API

The authorize() API is similar to the server-side authorize() API and it returns the same decision object. You can provide it additional options as shown below.

const decision = await KilpiClient.some.policy().authorize({
abortSignal, // Provide a custom abort signal for the fetch call
});

Caching

On top of deduplicating and batching queries for performance, KilpiClient also caches all requests for you automatically.

All authorization decisions are cached indefinitely, until the cache is invalidated.

Invalidating the cache

Use the KilpiClient.$cache API to invalidate the cache.

// Fully clears the cache
KilpiClient.$cache.invalidate();
// Fine-grained invalidation: Only invalidate this policy
KilpiClient.my.policy().invalidate();