usePolicies

The usePolicies hook is designed to validate policies against a given wallet address. It acts as a standalone tool for policy validation, similar to how the <GatedComponent/> gates content but is used directly within your React components to conditionally render or execute logic based on policy validation results.

Usage

To use the usePolicies hook, you must have a walletAddress and one or more policyIds. The hook will return the validation result, along with other useful status flags and data.

import React from 'react';
import { usePolicies } from '@ethpass/sdk';

const MyComponent = ({ walletAddress }) => {
  const { valid, data, isLoading, isError, error } = usePolicies(['policy1', 'policy2'], walletAddress);

  if (isLoading) return <div>Loading...</div>;
  if (isError) return <div>Error: {error.toString()}</div>;

  return (
    <div>
      {valid ? (
        <div>Access granted</div>
      ) : (
        <div>Access denied</div>
      )}
      {/* Optionally use data for more detailed rendering */}
    </div>
  );
};

 

Parameters

usePolicies accepts the following parameters:

  • policyIds (string[]): An array of policy identifiers. These are the policies that will be validated against the provided wallet address.
  • walletAddress (string): The wallet address to be validated against the specified policies. Ensure that this address is correctly formatted and non-empty.

Return Value

The usePolicies hook returns an object containing the following properties:

NameTypeDescription
validanyIndicates whether the wallet address satisfies all of the specified policies. The type of this property is based on your validation logic.
dataanyContains additional information returned from the policy validation process, typically including detailed results of the policy checks.
isLoadingbooleanA flag indicating if the policy validation request is currently in progress. This can be used to show a loading indicator in your UI.
isErrorbooleanA boolean flag that indicates if an error occurred during the policy validation process.
errorunknownThe error object or message, if an error occurred during validation. The type is unknown to accommodate various potential error structures.