@@ -17,28 +17,37 @@ import authProvider from "./authProvider";
17
17
18
18
const entrypoint = process .env .REACT_APP_API_ENTRYPOINT ;
19
19
const fetchHeaders = { Authorization: ` Bearer ${ window .localStorage .getItem (" token" )} ` };
20
- const fetchHydra = (url , options = {}) => baseFetchHydra (url, {
21
- ... options,
22
- headers: new Headers (fetchHeaders),
23
- });
24
- const apiDocumentationParser = entrypoint => parseHydraDocumentation (entrypoint, { headers: new Headers (fetchHeaders) })
25
- .then (
20
+ const fetchHydra = (url , options = {}) =>
21
+ localStorage .getItem (" token" )
22
+ ? baseFetchHydra (url, {
23
+ ... options,
24
+ headers: new Headers (fetchHeaders ()),
25
+ })
26
+ : baseFetchHydra (url, options);
27
+ const apiDocumentationParser = (entrypoint ) =>
28
+ parseHydraDocumentation (
29
+ entrypoint,
30
+ localStorage .getItem (" token" )
31
+ ? { headers: new Headers (fetchHeaders ()) }
32
+ : {}
33
+ ).then (
26
34
({ api }) => ({ api }),
27
35
(result ) => {
28
- switch (result .status ) {
29
- case 401 :
30
- return Promise .resolve ({
31
- api: result .api ,
32
- customRoutes: [
33
- < Route path= " /" render= {() => {
34
- return window .localStorage .getItem (" token" ) ? window .location .reload () : < Redirect to= " /login" / >
35
- }} / >
36
- ],
37
- });
38
-
39
- default :
40
- return Promise .reject (result);
36
+ if (result .status === 401 ) {
37
+ // Prevent infinite loop if the token is expired
38
+ localStorage .removeItem (" token" );
39
+
40
+ return Promise .resolve ({
41
+ api: result .api ,
42
+ customRoutes: [
43
+ < Route path= " /" render= {() => {
44
+ return localStorage .getItem (" token" ) ? window .location .reload () : < Redirect to= " /login" / >
45
+ }} / >
46
+ ],
47
+ });
41
48
}
49
+
50
+ return Promise .reject (result);
42
51
},
43
52
);
44
53
const dataProvider = baseHydraDataProvider (entrypoint, fetchHydra, apiDocumentationParser);
0 commit comments