These hooks decide what the UI shows. They call scute.authz as the signed-in user, so the app must allow client checks (the client_checks setting). Your server still checks the real action; see Authorization on the server.
useCan#
import { useCan } from "@scute/react-hooks";function EditButton({ id }: { id: string }) { const { allowed, needsStepUp, needsApproval, loading } = useCan("edit", `document:${id}`); if (loading) return null; if (allowed) return <button>Edit</button>; if (needsStepUp) return <button>Verify to edit</button>; if (needsApproval) return <button>Request approval</button>; return null;}useCan(action, resource?, { context?, enabled? }).
| Field | |
|---|---|
| allowed | true only for "allow" |
| needsStepUp | The answer is allow_with_step_up; see decision.step_up |
| needsApproval | The answer is allow_with_approval; see decision.approval |
| decision | The full AuthzDecision, or null |
| loading, error | Request state. error is { message, code? } or null. |
| refetch() | Check again |
Signed out, or with enabled: false, nothing is requested and allowed is false. The check runs again when the action, resource, context or user changes, and an answer for old inputs is never shown.
usePermissions#
import { usePermissions } from "@scute/react-hooks";function Toolbar() { const { has } = usePermissions(); return has("billing:export") ? <button>Export</button> : null;}usePermissions(resource?) returns { permissions, has, loading, error, refetch }. permissions is the AuthzPermissions answer; has(slug) is true when the user holds that permission outright (a step-up permission is in permissions.step_up, not in has).
useImpersonation#
For a banner while support is signed in as the user:
import { useImpersonation } from "@scute/react-hooks";function ImpersonationBanner() { const { impersonating, actor, expiresAt, stop, stopping } = useImpersonation(); if (!impersonating) return null; return ( <div role="status"> {actor?.email ?? actor?.name} is signed in as you until {expiresAt?.toLocaleTimeString()}. <button onClick={stop} disabled={stopping}>Stop</button> </div> );}| Field | |
|---|---|
| impersonating | The session's token has an impersonation act claim |
| actor | { kind, sub?, email?, name? } or null |
| expiresAt | When the session ends; it is never refreshed |
| stop() | Ends it and brings back the support person's own session when one was kept. Returns Promise<boolean>. |
| stopping | true while stop() runs |
See Impersonation for starting a session as a user.