Cerbos JavaScript SDK
    Preparing search index...

    Function useCheckResources

    • Check the principal's permissions on a set of resources.

      Parameters

      Returns AsyncResult<CheckResourcesResponse>

      import { useCheckResources } from "@cerbos/react";

      function SomeComponent() {
      const check = useCheckResources({
      resources: [
      {
      resource: {
      kind: "document",
      id: "1",
      attr: { owner: "user@example.com" },
      },
      actions: ["view", "edit"],
      },
      {
      resource: {
      kind: "document",
      id: "2",
      attr: { owner: "another-user@example.com" },
      },
      actions: ["view", "edit"],
      },
      ],
      });

      if (check.isLoading) {
      // show spinner
      return "Loading...";
      }

      if (check.error) {
      // handle error
      return "Error...";
      }

      return (
      <div>
      {check.data.allAllowed({
      kind: "document",
      id: "1",
      }) && <button>a button document 1</button>}
      {check.data.allAllowed({
      kind: "document",
      id: "2",
      }) && <button>a button document 2</button>}
      {check.data.isAllowed({
      resource: { kind: "document", id: "1" },
      action: "edit",
      }) && <button>another button for document 1</button>}
      {check.data.isAllowed({
      resource: { kind: "document", id: "2" },
      action: "edit",
      }) && <button>another button for document 2</button>}
      </div>
      );
      }