--- layout: default title: "CanAccess" storybook_path: ra-core-auth-canaccess--basic --- # `` This component calls the `authProvider.canAccess()` method on mount for a provided resource and action (and optionally a record). It will only display its children when users are authorized. By default, it will redirect users to `/authentication-error` if an error occurs. ## Usage The following form only displays the `role` field if the user has the permission to perform the `edit` action on the `users.role` resource: ```jsx import { CanAccess, Edit, SelectInput, SimpleForm, TextInput } from 'react-admin'; const UserEdit = () => ( ); ``` `` will call the `authProvider.canAccess()` method with the following parameters: `{ action: "edit", resource: "users.role", record: {} }` where `record` will be the currently edited record. ## Parameters `` expects the following props: | Name | Required | Type | Default | Description | | -------------- | -------- | -------------- | --------------------- | --- | | `action` | Required | `string` | - | The action to check, e.g. 'read', 'list', 'export', 'delete', etc. | | `resource` | | `string` | ResourceContext value | The resource to check, e.g. 'users', 'comments', 'posts', etc. | | `record` | | `object` | RecordContext value | The record to check. If passed, the child only renders if the user has access to that record, e.g. `{ id: 123, firstName: "John", lastName: "Doe" }` | | `loading` | | `ReactNode` | - | The element displayed while the `canAccess` call is pending | | `accessDenied` | | `ReactNode` | - | The element displayed when users are denied access to the resource | | `error` | | `ReactNode` | - | The element displayed when an error occurs while calling `authProvider.canAccess` | ## Securing Custom Routes By default, there is no authentication or authorization control on custom routes. If you need to restrict access to a custom route, wrap the content with ``. Remember to check the authentication status before with ``: ```tsx import { Authenticated, CanAccess, AccessDenied } from 'react-admin'; export const LogsPage = () => ( }> ... ); ``` Use the [``](./CustomRoutes.md) component to add custom routes to your admin. ```tsx import { Admin, CustomRoutes, Authenticated, CanAccess, AccessDenied, Layout } from 'react-admin'; import { Route } from 'react-router-dom'; import { LogsPage } from './LogsPage'; import { MyMenu } from './MyMenu'; const MyLayout = (props) => ; const App = () => ( } /> ); ``` Remember to also wrap your [custom menu items](./Menu.md) with `` to hide the menu items if the user doesn't have access to the resource. ```tsx import { Menu, CanAccess } from "react-admin"; import SsidChartIcon from "@mui/icons-material/SsidChart"; export const MyMenu = () => ( } /> ); ``` **Note**: You don't need to use `` on the core react-admin page components (``, ``, ``, ``) because they already have built-in access control. **Note**: You don't need to use `` on custom pages if your admin uses [`requireAuth`](./Admin.md#requireauth). ## Access Denied Message By default, `` renders nothing when the user doesn't have access to the resource. On custom pages, it's preferable to show an error message instead. Set the `accessDenied` prop to render a custom component in case of access denial: ```tsx import { Authenticated, CanAccess, AccessDenied } from 'react-admin'; export const LogsPage = () => ( }> ... ); ``` ![Access Denied](./img/accessDenied.png)