Overview
TheuseIsUserEntitledTo hook is a custom React hook designed to check a user’s entitlement status for a specific feature or service. It provides a function to make an API call to verify entitlements, along with loading and error states.
Usage
Parameters
authToken(string, required): The authentication token for API requests.
Returns
An object with the following properties:isUserEntitledTo(function): A function to check the user’s entitlement status.loading(boolean): Indicates whether an entitlement check is currently in progress.error(string | null): Contains an error message if an error occurred during the process, otherwise null.
isUserEntitledTo Function
TheisUserEntitledTo function is used to check the user’s entitlement status for a specific feature.
Parameters
payload(object, required): An object containing the entitlement details.entitlement(string, required): The name or identifier of the entitlement to check.usage(number, optional): The usage amount to check against the entitlement. Defaults to 1.
Returns
This function returns a Promise that resolves to the entitlement status response from the API.Example
API Details
The hook internally uses the following API endpoint:- URL:
${Config.BILLING_API_URL}/api/v1/entitlements/status - Method: POST
- Headers:
- ‘Content-Type’: ‘application/json’
- ‘Authorization’:
Bearer ${authToken}
- Body: JSON string containing
entitlementandusage
Error Handling
If an error occurs during the API call, theerror state will be set to ‘Failed to get User Entitlement status’. You should handle this in your component to display an appropriate error message to the user.
Performance Considerations
- The hook uses
useStateto manage its internal loading and error states. - The API call is made only when the
isUserEntitledTofunction is explicitly called, preventing unnecessary requests.
Dependencies
This hook depends on the following:- A
Configobject withBILLING_API_URLdefined