Skip to content

HydraAdmin component isn't working with no entrypoint props #31

@Lctrs

Description

@Lctrs

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 :

capture d ecran de 2017-07-12 12-32-42

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions