@styra/opa-react
Version:
Styra-supported React hooks and components for frontend authorization based on @styra/opa
50 lines • 1.53 kB
JavaScript
import useAuthz from "./use-authz.js";
/**
* Conditionally renders components based on authorization decisions for a specified
* policy path and input for the current user.
*
* The simplest use looks like that shown below; just wrap some arbitrary content
* and specify path and input.
*
* @example JSX children with fallback for the "denied" state
*
* ```tsx
* <Authz
* path={path}
* input={input}
* fallback={<div>unauthorized</div>}
* loading={<div>loading...</div>}>
* <Button>Delete Item</Button>
* </Authz>
* ```
*
* @example Unwrapping a policy evaluation result
*
* Assuming the policy returns an object, `{"result": true}`, the `fromResult` prop can be
* used to unwrap that:
*
* ```tsx
* <Authz
* path={path}
* input={input}
* fromResult={({result}) => result ?? false}
* <Button>Delete Item</Button>
* </Authz>
* ```
*
* ## Configuration
*
* Configuration involves defining an API endpoint for authorization along with a context
* that can be used to access authorization decisions throughout the application.
* The `<AuthzProvider/>` wrapper needs to be as high as possible in the component tree,
* since `<Authz/>` (or `useAuthz`) may only be used inside that wrapper.
*
*/
export default ({ children, path, loading, input, fallback = null, fromResult, }) => {
const { result, isLoading } = useAuthz(path, input, fromResult);
if (isLoading) {
return loading;
}
return !!result ? children : fallback;
};
//# sourceMappingURL=authz.js.map