diff --git a/docs/CanAccess.md b/docs/CanAccess.md index abd2d0f36a..07f9350b58 100644 --- a/docs/CanAccess.md +++ b/docs/CanAccess.md @@ -42,4 +42,59 @@ const UserEdit = () => ( | `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). diff --git a/docs/Permissions.md b/docs/Permissions.md index 2c2ed7b8f2..98bac63390 100644 --- a/docs/Permissions.md +++ b/docs/Permissions.md @@ -253,30 +253,59 @@ const CommentsToolbar = ({ record }) => ( ### Custom Routes -By default, there is no authentication or authorization control on the custom routes. If you need to restrict access to a custom route, you can use the `` component. Remember to check the authentication status before with ``: +By default, there is no authentication or authorization control on custom routes. If you need to restrict access to a custom route, you can use the `` component. Remember to check the authentication status before with ``: ```tsx -import { Admin, CustomRoutes, Authenticated, CanAccess } from 'react-admin'; +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). +**Note**: You don't need to use `` on custom pages if your admin uses [`requireAuth`](./Admin.md#requireauth). ## Permissions