Skip to main content

usePermission, useZanzibar, useAbac

Hooks that ask LumoAuth whether the signed-in person may do something, with one hook for each authorization model.

ComponentReadyView source
Works with
ReactNext.js

In this preview the sample person holds documents.edit, is a viewer of the Q3 report, and may read documents. The person does not hold billing.manage.

Installation​

npm install @lumoauth/react

Usage​

import { usePermission } from '@lumoauth/react';

export function DeleteButton({ onDelete }) {
const { allowed, isLoading } = usePermission('documents.delete');
if (isLoading || !allowed) return null;
return <button onClick={onDelete}>Delete</button>;
}

To wrap a block of UI, Protect does the same with less code. These hooks hide UI only: check again in your API.

Hooks​

Each hook returns { allowed, isLoading }.

ArgumentTypeDescription
usePermission(slug)stringA permission of the roles and permissions model, for example documents.edit.
useZanzibar(request){ object, relation, subject? }A relationship check, for example viewer of document:q3-report.
useAbac(request){ resourceType, action, resourceId? }An attribute policy check. Needs the scope abac:read on the provider.

The client behind the hooks​

useLumoAuth() returns the LumoAuth client of @lumoauth/client, already set up with the token of the session. Use it for calls that have no hook, such as a bulk check for a table of rows.

import { useLumoAuth } from '@lumoauth/react';

const lumo = useLumoAuth();
const results = await lumo.permissions.checkBulk(['documents.edit', 'documents.delete']);