Skip to main content

Show and hide UI by permission

Hide what a person may not use, with a component or a hook for each authorization model.

RecipeDraftView source
This page is a draft. The code exists, and not all of it has been run for this page yet. The page says what was checked.
Works with
JavaScript / TypeScriptReact

What it shows​

  1. Protect wraps the editor toolbar. A person without documents.edit sees a fallback text instead.
  2. The hook usePermission decides one control: the Save button is disabled without the permission.
  3. Two more blocks use a relationship and an attribute policy: is the person a viewer of document:q4-report, and may the person read documents?
  4. Your API checks the same permission again, because a person can change what a browser shows.

Each check asks LumoAuth with the access token of the signed-in person. The page needs no API key.

Before you start​

  • An application for a browser app, with its client ID. A new organization has the Starter App, which accepts http://localhost:5173.
  • The scope abac:read in the scopes of the application, for the attribute policy check. Without it, the sign-in fails with invalid_scope. Add it in the portal on the Configuration tab of the application, or with PUT /admin/clients/{clientId}/scopes.
  • The permission documents.edit, a role that holds it, and a user who holds the role. The setup script of Protect routes in each framework creates them.
  • For the relationship block: a tuple that makes the user a viewer of document:q4-report. Share a resource with relationships shows the namespace and the tuples.
  • For the attribute block: a policy that allows the action read on the resource type document. See Decide by attributes.
VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_CLIENT_IDThe client ID of the application. A browser app has no client secret.
PERMISSIONOptional. The permission that the toolbar and the Save button need. The default is documents.edit.

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization. A real browser signed in as each of two users. The test used the built-in permission users.view. Neither user had a relationship tuple or a matching attribute policy, so the relationship and attribute checks were seen to answer no only.

authorization/show-and-hide-ui/js/src/App.jsx
// A document page that shows each control only to a person who may use it.
// Each check asks LumoAuth with the access token of the signed-in person.
import { Protect, SignedIn, SignedOut, SignInButton, usePermission, useUser } from '@lumoauth/react';

const permission = import.meta.env.PERMISSION;

// A hook decides one control. The button stays, but it is disabled.
function SaveButton() {
const { allowed, isLoading } = usePermission(permission);
return (
<button disabled={isLoading || !allowed} onClick={() => alert('Your API checks the permission again.')}>
Save
</button>
);
}

function Document() {
const user = useUser();
return (
<>
<p>Signed in as {user?.name ?? user?.email}</p>
<h1>Q4 report</h1>

{/* Protect wraps a block. Without the permission, the fallback shows. */}
<Protect permission={permission} fallback={<p>Read only: you may not edit documents.</p>}>
<p>Editor toolbar: bold, italic, insert table</p>
</Protect>
<SaveButton />

{/* A relationship: is this person a viewer of this one document? */}
<Protect zanzibar={{ object: 'document:q4-report', relation: 'viewer' }} fallback={<p>Not shared with you.</p>}>
<p>Shared with you: comments of the finance team</p>
</Protect>

{/* An attribute policy: may this person read documents? */}
<Protect abac={{ resourceType: 'document', action: 'read' }} fallback={<p>The policy hides the history.</p>}>
<p>Version history: 12 versions</p>
</Protect>
</>
);
}

export default function App() {
return (
<main>
<SignedOut>
<p>Nobody is signed in.</p>
<SignInButton />
</SignedOut>
<SignedIn>
<Document />
</SignedIn>
</main>
);
}
authorization/show-and-hide-ui/js/src/main.jsx
// Show and hide UI by permission, relationship and attribute policy, with React.
//
// cd authorization/show-and-hide-ui/js && npm install && npm run dev
//
// Then open http://localhost:5173.
import { createRoot } from 'react-dom/client';
import { AuthCallback, LumoAuthProvider } from '@lumoauth/react';
import App from './App.jsx';

const callbackPath = '/auth/callback';

createRoot(document.getElementById('root')).render(
<LumoAuthProvider
domain={import.meta.env.LUMOAUTH_URL}
orgId={import.meta.env.LUMOAUTH_ORG_ID}
clientId={import.meta.env.LUMOAUTH_CLIENT_ID}
redirectUri={window.location.origin + callbackPath}
// The attribute policy check needs abac:read.
scope="openid profile email abac:read"
>
{window.location.pathname === callbackPath ? <AuthCallback afterSignInUrl="/" /> : <App />}
</LumoAuthProvider>,
);
authorization/show-and-hide-ui/js/vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

// The browser gets these values and nothing else from the environment. None
// of them is a secret: a browser app has no client secret and no API key.
export default defineConfig({
plugins: [react()],
define: {
'import.meta.env.LUMOAUTH_URL': JSON.stringify(process.env.LUMOAUTH_URL ?? 'https://app.lumoauth.dev'),
'import.meta.env.LUMOAUTH_ORG_ID': JSON.stringify(process.env.LUMOAUTH_ORG_ID ?? ''),
'import.meta.env.LUMOAUTH_CLIENT_ID': JSON.stringify(process.env.LUMOAUTH_CLIENT_ID ?? ''),
'import.meta.env.PERMISSION': JSON.stringify(process.env.PERMISSION ?? 'documents.edit'),
},
});
cd authorization/show-and-hide-ui/js && npm install && npm run dev

Open http://localhost:5173.

Check the result​

Output
Reader:
Read only: you may not edit documents.
Save (disabled)
Not shared with you.
The policy hides the history.

Editor:
Editor toolbar: bold, italic, insert table
Save
Not shared with you.
The policy hides the history.
  • Sign in as a user without the role: the toolbar shows its fallback, and Save is disabled.
  • Sign in as the user who holds the role: the toolbar appears, and Save is enabled.

Limits​

  • This hides UI. It does not protect data. The API must refuse the request itself: see Protect routes in each framework.
  • Each Protect and each hook sends one request when the component appears. For a table with many rows, use one bulk check: see Check many permissions at once.
  • Protect shows a spinner while the answer is on its way, and the fallback when nobody is signed in.