UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

57 lines 2.04 kB
import { useEffect } from 'react'; import { useCanAccess, } from "./useCanAccess.js"; import { useBasename, useNavigate } from "../routing/index.js"; /** * A hook that calls the authProvider.canAccess() method for a provided resource and action (and optionally a record). * It redirects to the /access-denied page if the user doesn't have the required permissions. * It redirects to the /authentication-error page if the authProvider.canAccess throws an error. * * The return value updates according to the request state: * * - start: { isPending: true } * - success: { isPending: false } * - error: { error: [error from provider], isPending: false } * * Useful to enable or disable features based on users permissions. * * @param {Object} params Any params you want to pass to the authProvider * @param {string} params.resource The resource to check access for * @param {string} params.action The action to check access for * @param {Object} params.record Optional. The record to check access for * * @returns Return the react-query result. * * @example * import { useRequireAccess } from 'react-admin'; * * const PostDetail = () => { * const { isPending } = useRequireAccess({ * resource: 'posts', * action: 'read', * }); * if (isPending) { * return null; * } * * return <PostEdit />; * }; */ export const useRequireAccess = (params) => { const { canAccess, data, error, ...rest } = useCanAccess(params); const navigate = useNavigate(); const basename = useBasename(); useEffect(() => { if (rest.isPending) return; if (canAccess === false) { navigate(`${basename}/access-denied`); } }, [basename, canAccess, navigate, rest.isPending]); useEffect(() => { if (error) { navigate(`${basename}/authentication-error`); } }, [basename, navigate, error]); return rest; }; //# sourceMappingURL=useRequireAccess.js.map