Skip to content

Commit 648990a

Browse files
authored
docs: update authentication-support.md (#1484)
1 parent 0c6ff90 commit 648990a

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

admin/authentication-support.md

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ for more information.
66

77
In short, you have to tweak the data provider and the api documentation parser like this:
88

9-
```javascript
10-
// admin/src/App.js
9+
```typescript
10+
// pwa/pages/admin/index.tsx
1111

12+
import Head from "next/head";
1213
import { Redirect, Route } from "react-router-dom";
13-
import { HydraAdmin, hydraDataProvider as baseHydraDataProvider, fetchHydra as baseFetchHydra, useIntrospection } from "@api-platform/admin";
14+
import { hydraDataProvider as baseHydraDataProvider, fetchHydra as baseFetchHydra, useIntrospection } from "@api-platform/admin";
1415
import parseHydraDocumentation from "@api-platform/api-doc-parser/lib/hydra/parseHydraDocumentation";
15-
import authProvider from "./authProvider";
16+
import authProvider from "utils/authProvider";
17+
import { ENTRYPOINT } from "config/entrypoint";
1618

17-
const entrypoint = process.env.REACT_APP_API_ENTRYPOINT;
1819
const getHeaders = () => localStorage.getItem("token") ? {
1920
Authorization: `Bearer ${localStorage.getItem("token")}`,
2021
} : {};
@@ -32,40 +33,51 @@ const RedirectToLogin = () => {
3233
}
3334
return <Redirect to="/login" />;
3435
};
35-
const apiDocumentationParser = async (entrypoint) => {
36+
const apiDocumentationParser = async () => {
3637
try {
37-
const { api } = await parseHydraDocumentation(entrypoint, { headers: getHeaders });
38+
const { api } = await parseHydraDocumentation(ENTRYPOINT, { headers: getHeaders });
3839
return { api };
3940
} catch (result) {
40-
if (result.status === 401) {
41-
// Prevent infinite loop if the token is expired
42-
localStorage.removeItem("token");
43-
44-
return {
45-
api: result.api,
46-
customRoutes: [
47-
<Route path="/" component={RedirectToLogin} />
48-
],
49-
};
41+
if (result.status !== 401) {
42+
throw result;
5043
}
5144

52-
throw result;
45+
// Prevent infinite loop if the token is expired
46+
localStorage.removeItem("token");
47+
48+
return {
49+
api: result.api,
50+
customRoutes: [
51+
<Route key="/" path="/" component={RedirectToLogin} />
52+
],
53+
};
5354
}
5455
};
5556
const dataProvider = baseHydraDataProvider({
56-
entrypoint,
57+
entrypoint: ENTRYPOINT,
5758
httpClient: fetchHydra,
5859
apiDocumentationParser,
59-
mercure: true, // or false if you don't use Mercure
6060
});
6161

62-
export default () => (
63-
<HydraAdmin
64-
dataProvider={ dataProvider }
65-
authProvider={ authProvider }
66-
entrypoint={ entrypoint }
67-
/>
62+
const AdminLoader = () => {
63+
if (typeof window !== "undefined") {
64+
const { HydraAdmin } = require("@api-platform/admin");
65+
return <HydraAdmin dataProvider={dataProvider} authProvider={authProvider} entrypoint={window.origin} />;
66+
}
67+
68+
return <></>;
69+
};
70+
71+
const Admin = () => (
72+
<>
73+
<Head>
74+
<title>API Platform Admin</title>
75+
</Head>
76+
77+
<AdminLoader />
78+
</>
6879
);
80+
export default Admin;
6981
```
7082

7183
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).

0 commit comments

Comments
 (0)