Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove permissions prop injection from main views #6921

Merged
merged 5 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,17 @@ test('MyComponent', () => {
});
```

## `useAuthenticated` Signature hsa Changed

`useAuthenticated` uses to accept only the parameters passed to the `authProvider.checkAuth` function. It now accept an option object with two properties:
- `enabled`: whether it should check for an authenticated user
- `params`: the parameters to pass to `checkAuth`

```diff
- useAuthenticated('permissions.posts.can_create');
+ useAuthenticated({ params: 'permissions.posts.can_create' })
```

# Upgrade to 3.0

We took advantage of the major release to fix all the problems in react-admin that required a breaking change. As a consequence, you'll need to do many small changes in the code of existing react-admin v2 applications. Follow this step-by-step guide to upgrade to react-admin v3.
Expand Down
35 changes: 33 additions & 2 deletions docs/Authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,15 @@ const MyPage = () => {
export default MyPage;
```

If you call `useAuthenticated()` with a parameter, this parameter is passed to the `authProvider` call as second parameter. that allows you to add authentication logic depending on the context of the call:
`useAuthenticated` accepts an options object as its only argument, with the following properties:
- `enabled`: whether it should check for an authenticated user
- `params`: the parameters to pass to `checkAuth`

If you call `useAuthenticated()` with a `params` option, those parameters are passed to the `authProvider.checkAuth` call. That allows you to add authentication logic depending on the context of the call:

```jsx
const MyPage = () => {
useAuthenticated({ foo: 'bar' }); // calls authProvider.checkAuth({ foo: 'bar' })
useAuthenticated({ params: foo: 'bar' } }); // calls authProvider.checkAuth({ foo: 'bar' })
return (
<div>
...
Expand Down Expand Up @@ -1165,3 +1169,30 @@ const Menu = ({ onMenuClick, logout }) => {
);
}
```

### Allowing Anonymous Access to Custom Views

You might have custom views that leverages our components or hooks such as:
- `Create`, `CreateBase` `CreateController` and `useCreateController`
- `Edit`, `EditBase`, `EditController` and `useEditController`
- `List`, `ListBase`, `ListController` and `useListController`
- `Show`, `ShowBase`, `ShowController` and `useShowController`

By default, they all redirect anonymous users to the login page. You can disable this behavior by passing the `disableAuthentication` boolean prop:

```jsx
const MostRecentComments = () => {
const { data, loaded } = useListController({
disableAuthentication: true,
resource: 'comments',
sort: { field: 'created_at', order: 'desc' },
perPage: 10
});

if (!loaded) {
return null;
}

return <CommentsList comments={data} />
}
```
2 changes: 1 addition & 1 deletion packages/ra-core/src/auth/useCheckAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const checkAuthWithoutAuthProvider = () => Promise.resolve();
*
* @return {Promise} Resolved to the authProvider response if the user passes the check, or rejected with an error otherwise
*/
type CheckAuth = (
export type CheckAuth = (
params?: any,
logoutOnFailure?: boolean,
redirectTo?: string,
Expand Down
24 changes: 10 additions & 14 deletions packages/ra-core/src/core/Resource.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import { useMemo } from 'react';
import { Route, Routes } from 'react-router-dom';

import { ResourceProps } from '../types';
Expand All @@ -8,17 +7,14 @@ import { ResourceContextProvider } from './ResourceContextProvider';
export const Resource = (props: ResourceProps) => {
const { create: Create, edit: Edit, list: List, name, show: Show } = props;

// match tends to change even on the same route ; using memo to avoid an extra render
return useMemo(() => {
return (
<ResourceContextProvider value={name}>
<Routes>
{Create && <Route path="create/*" element={<Create />} />}
{Show && <Route path=":id/show/*" element={<Show />} />}
{Edit && <Route path=":id/*" element={<Edit />} />}
{List && <Route path="/*" element={<List />} />}
</Routes>
</ResourceContextProvider>
);
}, [name, Create, Edit, List, Show]);
return (
<ResourceContextProvider value={name}>
<Routes>
{Create && <Route path="create/*" element={<Create />} />}
{Show && <Route path=":id/show/*" element={<Show />} />}
{Edit && <Route path=":id/*" element={<Edit />} />}
{List && <Route path="/*" element={<List />} />}
</Routes>
</ResourceContextProvider>
);
};