@@ -6,15 +6,16 @@ for more information.
6
6
7
7
In short, you have to tweak the data provider and the api documentation parser like this:
8
8
9
- ``` javascript
10
- // admin/src/App.js
9
+ ``` typescript
10
+ // pwa/pages/admin/index.tsx
11
11
12
+ import Head from " next/head" ;
12
13
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" ;
14
15
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" ;
16
18
17
- const entrypoint = process .env .REACT_APP_API_ENTRYPOINT ;
18
19
const getHeaders = () => localStorage .getItem (" token" ) ? {
19
20
Authorization: ` Bearer ${localStorage .getItem (" token" )} ` ,
20
21
} : {};
@@ -32,40 +33,51 @@ const RedirectToLogin = () => {
32
33
}
33
34
return <Redirect to =" /login" />;
34
35
};
35
- const apiDocumentationParser = async (entrypoint ) => {
36
+ const apiDocumentationParser = async () => {
36
37
try {
37
- const { api } = await parseHydraDocumentation (entrypoint , { headers: getHeaders });
38
+ const { api } = await parseHydraDocumentation (ENTRYPOINT , { headers: getHeaders });
38
39
return { api };
39
40
} 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 ;
50
43
}
51
44
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
+ };
53
54
}
54
55
};
55
56
const dataProvider = baseHydraDataProvider ({
56
- entrypoint,
57
+ entrypoint: ENTRYPOINT ,
57
58
httpClient: fetchHydra ,
58
59
apiDocumentationParser ,
59
- mercure: true , // or false if you don't use Mercure
60
60
});
61
61
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
+ < / >
68
79
);
80
+ export default Admin ;
69
81
```
70
82
71
83
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