You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
API Platform Admin delegates the authentication support to React Admin.
4
-
Refer to [the chapter dedicated to authentication in the React Admin documentation](https://marmelab.com/react-admin/Authentication.html)
5
-
for more information.
6
4
7
-
In short, you have to tweak the data provider and the API documentation parser like this:
5
+
Refer to the [Auth Provider Setup](https://marmelab.com/react-admin/Authentication.html) documentation for more information.
6
+
7
+
**Tip:** Once you have set up the authentication, you can also configure React Admin to perform client-side Authorization checks. Refer to the [Authorization](https://marmelab.com/react-admin/Permissions.html) documentation for more information.
8
+
9
+
## HydraAdmin
10
+
11
+
Enabling authentication support for [`<HydraAdmin>` component](./components.md#hydra) consists of a few parts, which need to be integrated together.
12
+
13
+
In the following steps, we will see how to:
14
+
15
+
- Make authenticated requests to the API (i.e. include the `Authorization` header)
16
+
- Redirect users to the login page if they are not authenticated
17
+
- Clear expired tokens when encountering unauthorized `401` response
18
+
19
+
### Make Authenticated Requests
20
+
21
+
First, we need to implement a `getHeaders` function, that will add the Bearer token from `localStorage` (if there is one) to the `Authorization` header.
Then, we'll create a `<RedirectToLogin>` component, that will redirect users to the `/login` route if no token is available in the `localStorage`, and call the dataProvider's `introspect` function otherwise.
Now, we will extend the `parseHydraDocumentaion` function (imported from the [@api-platform/api-doc-parser](https://github.com/api-platform/api-doc-parser) library).
67
+
68
+
We will customize it to clear expired tokens when encountering unauthorized `401` response.
Now, we can initialize the Hydra data provider with the custom `fetchHydra` (with custom headers) and `apiDocumentationParser` functions created earlier.
For the implementation of the admin component, you can find a working example in the [API Platform's demo application](https://github.com/api-platform/demo/blob/4.0/pwa/components/admin/Admin.tsx).
172
+
173
+
## OpenApiAdmin
174
+
175
+
This section explains how to set up and customize the [`<OpenApiAdmin>` component](./components.md/#openapi) to enable authentication.
176
+
177
+
In the following steps, we will see how to:
178
+
179
+
- Make authenticated requests to the API (i.e. include the `Authorization` header)
180
+
- Implement an authProvider to redirect users to the login page if they are not authenticated, and clear expired tokens when encountering unauthorized `401` response
181
+
182
+
### Making Authenticated Requests
183
+
184
+
First, we need to create a custom `httpClient` to add authentication tokens (via the the `Authorization` HTTP header) to requests.
185
+
186
+
We will then configure `openApiDataProvider` to use [`ra-data-simple-rest`](https://github.com/marmelab/react-admin/blob/master/packages/ra-data-simple-rest/README.md), a simple REST dataProvider for React Admin, and make it use the `httpClient` we created earlier.
**Note:** The `simpleRestProvider` provider expect the API to include a `Content-Range` header in the response. You can find more about the header syntax in the [Mozilla’s MDN documentation: Content-Range](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range).
213
+
214
+
**Note:** The `getAccessToken` function retrieves the JWT token stored in the browser's localStorage. Replace it with your own logic in case you don't store the token that way.
215
+
216
+
### Creating The AuthProvider
217
+
218
+
Now let's create and export an `authProvider` object that handles authentication and authorization logic.
For the implementation of the auth provider, you can find a working example in the [API Platform's demo application](https://github.com/api-platform/demo/blob/main/pwa/utils/authProvider.tsx).
280
+
### Updating The Admin Component
281
+
282
+
Finally, we can update the `Admin` component to use the `authProvider` and `dataProvider` we created earlier.
283
+
284
+
```tsx
285
+
// src/Admin.tsx
286
+
287
+
import {OpenApiAdmin} from '@api-platform/admin';
288
+
import authProvider from "./authProvider";
289
+
import dataProvider from "./dataProvider";
290
+
import {API_DOCS_PATH, API_ENTRYPOINT_PATH} from "./config/api";
0 commit comments