Skip to content

Commit 0ffd027

Browse files
jordanaubertalanpoulain
authored andcommitted
Fix Admin Authentication Support (#788)
* Fix Admin Authentication Support * Add missing import * Keep entrypoint var
1 parent 009fb4b commit 0ffd027

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

admin/authentication-support.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,38 +62,38 @@ import React from 'react';
6262
import parseHydraDocumentation from '@api-platform/api-doc-parser/lib/hydra/parseHydraDocumentation';
6363
import { HydraAdmin, hydraClient, fetchHydra as baseFetchHydra } from '@api-platform/admin';
6464
import authProvider from './authProvider';
65-
import { Redirect } from 'react-router-dom';
65+
import { Route, Redirect } from 'react-router-dom';
6666

6767
const entrypoint = 'https://demo.api-platform.com'; // Change this by your own entrypoint
68-
const fetchHeaders = {'Authorization': `Bearer ${window.localStorage.getItem('token')}`};
68+
const fetchHeaders = {'Authorization': `Bearer ${localStorage.getItem('token')}`};
6969
const fetchHydra = (url, options = {}) => baseFetchHydra(url, {
7070
...options,
7171
headers: new Headers(fetchHeaders),
7272
});
7373
const dataProvider = api => hydraClient(api, fetchHydra);
74-
const apiDocumentationParser = entrypoint => parseHydraDocumentation(entrypoint, { headers: new Headers(fetchHeaders) })
75-
.then(
76-
({ api }) => ({ api }),
77-
(result) => {
78-
switch (result.status) {
79-
case 401:
80-
return Promise.resolve({
81-
api: result.api,
82-
customRoutes: [{
83-
props: {
84-
path: '/',
85-
render: () => <Redirect to={`/login`}/>,
86-
},
87-
}],
88-
});
89-
90-
default:
91-
return Promise.reject(result);
92-
}
93-
},
94-
);
95-
96-
export default props => (
74+
const apiDocumentationParser = entrypoint =>
75+
parseHydraDocumentation(entrypoint, {
76+
headers: new Headers(fetchHeaders),
77+
}).then(
78+
({ api }) => ({ api }),
79+
result => {
80+
const { api, status } = result;
81+
82+
if (status === 401) {
83+
return Promise.resolve({
84+
api,
85+
status,
86+
customRoutes: [
87+
<Route path="/" render={() => <Redirect to="/login" />} />,
88+
],
89+
});
90+
}
91+
92+
return Promise.reject(result);
93+
}
94+
);
95+
96+
export default () => (
9797
<HydraAdmin
9898
apiDocumentationParser={apiDocumentationParser}
9999
authProvider={authProvider}

0 commit comments

Comments
 (0)