Skip to content

Getting started

This guide adds a Policy module to a Layeron backend app and evaluates a decision.

Terminal window
import { policy } from "@layeron/modules"
Terminal window
const appPolicy = policy({
name: "app",
rules: [{
id: "admins-read",
effect: "allow",
subjects: ["role:admin"],
actions: ["resource.read"],
resources: ["resource:*"],
}],
})

The policy name is stable within the environment. Rules match subjects, actions, resources, and optional conditions.

Terminal window
app.use(appPolicy)

Layeron records the policy in the app specification, compiles it into a PolicyManifest, provisions the policy store, and creates the Policy Product Worker.

Terminal window
const decision = await appPolicy.evaluate({
subject: {
kind: "user",
id: "user_1",
roles: ["admin"],
},
action: "resource.read",
resource: {
type: "resource",
id: "resource_123",
},
})

Use can(...) when you need a boolean check and require(...) when you want Policy to throw on denial.