@availity/authorize
Version:
Check user permissions to see if the current user is authorized to see your content.
68 lines (63 loc) • 5.1 kB
text/typescript
import { ReactNode } from 'react';
import { UseQueryOptions } from '@tanstack/react-query';
type Permissions = {
/** - **string**: The permission ID, eg: '1234'
* - **array**: The array can contain Permission ID's as well as other arrays which contain Permission ID's
* eg: ['1234', '2345', ['3456', '4567'], ['5678', '6789']]. The items in a nested array indicate
* Permission ID's that must all be granted to the user to be considered authorized - they act as an "AND".
* The items in the top of the array act as an "OR" - if any are granted to the user, the user is considered
* authorized. For example, ['1234', '2345', ['3456', '4567'], ['5678', '6789']] is equivalent to
* '1234' || '2345' || ('3456' && '4567') || ('5678' && '6789').
*/
permissions: (string | string[])[];
};
type Parameters = {
/** Additional parameters
* - **`organizationId`**: String. Optional. When present, the permission is validated to ensure it is assigned to the organization.
* - **`customerId`**: String. Optional. When present, the permission is validated to ensure it is assigned to the customer.
* - **`region`**: String or Boolean. Optional. Default: `true`. When a string, the permission is validated to ensure it is assigned in the provided region. When true, the permission is validated to ensure it is assigned in the current region.
* - **`resources`**: (string | string[])[]. Optional.
* - **string**: The Resource ID, eg: `'12345'`
* - **array**: The array can contain Resource ID's as well as other arrays which contain Resource ID's eg: `['12345', '23456', ['34567', '45678'], ['56789', '67890']]`. The items in a nested array indicate the Resource ID's that must _all_ be granted to the user to be considered authorized - they act as an `"AND"`. The items in the top of the array act as an `"OR"` - if _any_ are granted to the user, the user is considered authorized. For example, `['12345', '23456', ['34567', '45678'], ['56789', '67890']]` is equivalent to `'12345' || '23456' || ('34567' && '45678') || ('56789' && '67890')`.
*/
parameters?: {
/** When present, the permission is validated to ensure it is assigned to the organization. */
organizationId?: string;
/** When present, the permission is validated to ensure it is assigned to the customer. */
customerId?: string;
/** When a string, the permission is validated to ensure it is assigned in the provided region.
* When true, the permission is validated to ensure it is assigned in the current region.
*/
region?: boolean | string;
/** - **string**: The permission ID, eg: `'1234'`
* - **array**: The array can contain Permission ID's as well as other arrays which contain Permission ID's
* eg: `['1234', '2345', ['3456', '4567'], ['5678', '6789']]`. The items in a nested array indicate
* Permission ID's that must all be granted to the user to be considered authorized - they act as an `"AND"`.
* The items in the top of the array act as an `"OR"` - if any are granted to the user, the user is considered
* authorized. For example, `['1234', '2345', ['3456', '4567'], ['5678', '6789']]` is equivalent to
* `'1234' || '2345' || ('3456' && '4567') || ('5678' && '6789')`.
*/
resources?: (string | string[])[];
};
};
type Options = {
/** React Query Options */
queryOptions?: Omit<UseQueryOptions<boolean, unknown, boolean, (string | boolean | (string | string[])[] | undefined)[]>, 'queryKey' | 'queryFn'>;
};
type UseAuthorizeProps = Permissions & Parameters & Options;
declare const useAuthorize: (permissions: UseAuthorizeProps["permissions"], parameters?: UseAuthorizeProps["parameters"], options?: UseAuthorizeProps["queryOptions"]) => {
authorized: boolean;
isLoading: boolean;
};
type AuthorizeProps = {
/** The content that renders when the user does have the permissions required. */
children?: ReactNode;
/** When true, BlockUi is used when loading the permissions. When a node, that node is rendered instead of BlockUi when loading the permissions. When false, nothing is rendered when loading the permissions. Default: true. */
loader?: boolean | ReactNode;
/** Negate the authorization. If the user does have the permissions specified, they are considered "unauthorized" (shown the unauthorized prop content). If the user does not have the permissions specified, they are considered "authorized" (shown the children prop content) */
negate?: boolean;
/** The content that renders when the user does not have the permissions required. */
unauthorized?: ReactNode;
} & Pick<UseAuthorizeProps, 'permissions' | 'queryOptions'> & UseAuthorizeProps['parameters'];
declare const Authorize: ({ permissions, resources, customerId, organizationId, region, loader, negate, children, unauthorized, queryOptions, }: AuthorizeProps) => JSX.Element | null;
export { type AuthorizeProps, type UseAuthorizeProps, Authorize as default, useAuthorize };