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

Fix use auth provider in docs #7927

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 9 additions & 3 deletions docs/Tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -699,12 +699,14 @@ By default, react-admin displays the list page of the first `Resource` element a
import * as React from "react";
import { Card, CardContent, CardHeader } from '@mui/material';

export default () => (
const Dashboard = () => (
<Card>
<CardHeader title="Welcome to the administration" />
<CardContent>Lorem ipsum sic dolor amet...</CardContent>
</Card>
);

export default Dashboard;
```

```jsx
Expand Down Expand Up @@ -732,7 +734,7 @@ The `authProvider` must expose 5 methods, each returning a `Promise`:

```jsx
// in src/authProvider.js
export default {
const authProvider = {
// called when the user attempts to log in
login: ({ username }) => {
localStorage.setItem('username', username);
Expand Down Expand Up @@ -761,6 +763,8 @@ export default {
// called when the user navigates to a new location, to check for permissions / roles
getPermissions: () => Promise.resolve(),
};

export default authProvider;
```

**Tip**: As the `authProvider` calls are asynchronous, you can easily fetch an authentication server in there.
Expand Down Expand Up @@ -889,7 +893,7 @@ import { stringify } from 'query-string';
const apiUrl = 'https://my.api.com/';
const httpClient = fetchUtils.fetchJson;

export default {
const dataProvider= {
getList: (resource, params) => {
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
Expand Down Expand Up @@ -976,6 +980,8 @@ export default {
}).then(({ json }) => ({ data: json }));
}
};

export default dataProvider;
```

**Tip**: `fetchUtils.fetchJson()` is just a shortcut for `fetch().then(r => r.json())`, plus a control of the HTTP response code to throw an `HTTPError` in case of 4xx or 5xx response. Feel free to use `fetch()` directly if it doesn't suit your needs.
Expand Down
4 changes: 2 additions & 2 deletions docs/useAuthProvider.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const UserName = ({ userId }) => {
const [loading, setLoading] = useState(true);
const [error, setError] = useState();
useEffect(() => {
useAuthProvider.getIdentity()
authProvider.getIdentity()
.then(({ data }) => {
setIdentity(data);
setLoading(false);
Expand All @@ -30,7 +30,7 @@ const UserName = ({ userId }) => {
setError(error);
setLoading(false);
})
}, []);
}, [authProvider]);

if (loading) return <Loading />;
if (error) return <Error />;
Expand Down