Skip to content

Protecting pages

Protecting pages

Protecting pages is easy with Kilpi. Pages are easy to protect based on

  1. An explicit policy, e.g. await Kilpi.authorize("my:policy").
  2. According to data accessed on the page, e.g. await listDocuments.protect() (using protected queries).

Use Kilpi.onUnauthorized to redirect to users away from page to any other page.

See example below for Next.js applications.

export default async function Page() {
Kilpi.onUnauthorized(() => redirect("/login"));
// (a) Authorize user to page
await Kilpi.authorize("my:policy");
// (b) Allow only if has access to data on page (using protected queries)
const doc = await listDocuments.protect()
return <div>...</div>
}