Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Lctrs opened this issue Jul 12, 2017 · 2 comments
Closed

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

Lctrs opened this issue Jul 12, 2017 · 2 comments

Comments

@Lctrs
Copy link

Lctrs commented Jul 12, 2017

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

@eloyekunle
Copy link

Hey guys,
I'm experiencing this same problem. Can someone look into it?

@dunglas
Copy link
Member

dunglas commented Sep 7, 2017

Closed by #51.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants