API Reference

Kizuna.roles

Declare the roles callers hold, as names or built from a permission catalog.

Alpha

The least settled part of kizuna. Kizuna.roles may change shape, move out of the contract, or be removed before v2. Pin your version, and expect to rewrite what you build on it.

Kizuna.roles(names) declares the roles callers hold:

roles.ts
import { Kizuna } from '@ts-kizuna/core';

export const roles = Kizuna.roles(['member', 'admin', 'owner']);

Put them on the identity whose callers hold them, under roles. Its guard returns role, one name or an array of them, and a route's roles in the access control map is checked against it before the handler runs.

PropertyDescription
roles.namesThe role names, in declaration order.
roles.schemaA zod enum of the names, for a response that tells a client who it is.

From a permission catalog

Kizuna.roles(permissions, definitions) builds the roles from a permission catalog, so a route can name the permission it requires instead of the role. Every entry is checked against the catalog, and 'all' means every permission declared:

roles.ts
export const roles = Kizuna.roles(permissions, {
    member: {
        workspace: ['read'],
        project: ['read', 'update'],
    },
    admin: {
        workspace: ['read', 'update'],
        project: ['create', 'read', 'update', 'delete', 'share'],
    },
    owner: 'all',
});

A route's requires is checked against what the caller's role holds, or the subset the guard returns as permissions, and the handler reads the list under auth.member.permissions.

PropertyDescription
roles.permissionsThe catalog the roles are built from.
roles.definitionsEach role and what it holds, as declared.
roles.of(role)What one role holds, or the union for several.
roles.holds(role, requires)Whether the role holds every permission in requires outright.

See Access Control for the walkthrough.

On this page