Skip to main content
👀 Interested in the latest enterprise backend features of refine? 👉 Join now and get early access!
Version: 4.xx.xx
Source Code

usePermissions

CAUTION

This hook can only be used if the authProvider is provided.

usePermissions calls the getPermissions method from the authProvider under the hood.

It returns the result of react-query's useQuery which includes many properties, some of which being isSuccess and isError.

Data that is resolved from the getPermissions will be returned as the data in the query result.

Usage​

usePermissions can be useful when you want to get user's permission's anywhere in your code.

For example, if you want only the users with the admin role to see the create button in a list page, we have a logic in authProvider's getPermissions method like below:

import type { AuthBindings } from "@refinedev/core";

const authProvider: AuthBindings = {
...
getPermissions: async () => {
return ["admin"];
},
...
};

Get permissions data in the list page with usePermissions and check if the user has "admin" role:

pages/post/list
import { usePermissions } from "@refinedev/core";
import { List } from "@refinedev/antd";

export const PostList: React.FC = () => {
const { data: permissionsData } = usePermissions();

return <List canCreate={permissionsData?.includes("admin")}>...</List>;
};

For more information, refer to the <List> documentation →

Last updated on Jul 25, 2023 by Yıldıray Ünlü