Protect
Shows its children only when the signed-in person passes an authorization check. The check can be a permission, a relationship or an attribute policy.
In this preview the sample person holds documents.edit and is a viewer of the Q3 report, and does not hold billing.manage.
Installation
- npm
- pnpm
- Yarn
- Bun
npm install @lumoauth/react
pnpm add @lumoauth/react
yarn add @lumoauth/react
bun add @lumoauth/react
Usage
import { Protect } from '@lumoauth/react';
<Protect permission="documents.edit" fallback={<ReadOnlyBanner />}>
<Editor />
</Protect>
Protect asks LumoAuth with the access token of the signed-in person, and shows a spinner until the answer arrives. It shows fallback when the answer is no and when nobody is signed in.
A person can change what a browser shows. Check the same permission again in your API before you return or change anything. The route protection recipe shows that check in each server SDK.
Three kinds of check
Give exactly one of the three props.
| Prop | Model | Question |
|---|---|---|
permission | Roles and permissions | Does a role of this person hold the permission documents.edit? |
zanzibar | Relationships | Is this person a viewer of document:q3-report? |
abac | Attribute policies | Do the policies allow this person to read a document, given the attributes of both? |
The abac check needs the scope abac:read. Add it to the scope prop of the provider.
Props
| Prop | Type | Description |
|---|---|---|
permission | string | The permission to check, for example documents.edit. |
zanzibar | { object: string; relation: string; subject?: string } | A relationship check. Without subject, the subject is the signed-in person. |
abac | { resourceType: string; action: string; resourceId?: string } | An attribute policy check. |
fallback | ReactNode | What to show when access is denied. |
childrenrequired | ReactNode | What to show when access is granted. |