-
-
Notifications
You must be signed in to change notification settings - Fork 132
Closed
Description
Hi,
I followed the docs to implement the admin with JWT Authentication support. Here is the code :
// authClient.js
import { AUTH_LOGIN, AUTH_LOGOUT, AUTH_ERROR, AUTH_CHECK } from 'admin-on-rest';
export default (type, params) => {
if (type === AUTH_LOGIN) {
const { username, password } = params;
const request = new Request(`${process.env.REACT_APP_ENTRYPOINT}/login_check`, {
method: 'POST',
body: JSON.stringify({ username, password }),
headers: new Headers({ 'Content-Type': 'application/json' }),
});
return fetch(request)
.then(response => {
if (response.status < 200 || response.status >= 300) {
throw new Error(response.statusText);
}
return response.json();
})
.then(({ token }) => {
localStorage.setItem('token', token);
});
}
if (type === AUTH_LOGOUT) {
localStorage.removeItem('token');
return Promise.resolve();
}
if (type === AUTH_ERROR) {
const { status } = params;
if (status === 401 || status === 403) {
localStorage.removeItem('token');
return Promise.reject();
}
return Promise.resolve();
}
if (type === AUTH_CHECK) {
return localStorage.getItem('token') ? Promise.resolve() : Promise.reject();
}
return Promise.reject('Unknown method.');
};// App.js
import React, { Component } from 'react';
import { HydraAdmin, hydraClient, fetchHydra } from 'api-platform-admin';
import authClient from './authClient';
const fetchWithAuth = (url, options = {}) => {
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/ld+json' });
}
options.headers.set(
'Authorization',
`Bearer ${localStorage.getItem('token')}`
);
return fetchHydra(url, options);
};
class App extends Component {
render() {
return (
<HydraAdmin
restClient={hydraClient(
process.env.REACT_APP_ENTRYPOINT,
fetchWithAuth
)}
authClient={authClient}
/>
);
}
}
export default App;Problem is that HydraAdmin component requires entrypoint prop to be set as it passes it to parseHydraDocumentation() in componentDidMount(). But the doc says that it is not required in this case.
So, I added it to my App's render method :
// App.js
// ...
class App extends Component {
render() {
return (
<HydraAdmin
entrypoint={process.env.REACT_APP_ENTRYPOINT}
restClient={hydraClient(
process.env.REACT_APP_ENTRYPOINT,
fetchWithAuth
)}
authClient={authClient}
/>
);
}
}Now, it hits my API backend but the login page still doesn't show up and I got this error in the console :
Metadata
Metadata
Assignees
Labels
No labels
