JavaScript SDK

Authorization hooks

useCan, usePermissions and useImpersonation from @scute/react-hooks.

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
allowedtrue only for "allow"
needsStepUpThe answer is allow_with_step_up; see decision.step_up
needsApprovalThe answer is allow_with_approval; see decision.approval
decisionThe full AuthzDecision, or null
loading, errorRequest 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
impersonatingThe session's token has an impersonation act claim
actor{ kind, sub?, email?, name? } or null
expiresAtWhen 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>.
stoppingtrue while stop() runs

See Impersonation for starting a session as a user.