Skip to content

Commit 2ef1ba6

Browse files
authored
fix: manage correctly the token not present in local storage (#1184)
1 parent d6f84f4 commit 2ef1ba6

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

admin/authentication-support.md

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,37 @@ import authProvider from "./authProvider";
1717

1818
const entrypoint = process.env.REACT_APP_API_ENTRYPOINT;
1919
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(
2634
({ api }) => ({ api }),
2735
(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+
});
4148
}
49+
50+
return Promise.reject(result);
4251
},
4352
);
4453
const dataProvider = baseHydraDataProvider(entrypoint, fetchHydra, apiDocumentationParser);

0 commit comments

Comments
 (0)