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

Authentication in react-admin #24

Open
sen0va opened this issue Jan 15, 2019 · 5 comments
Open

Authentication in react-admin #24

sen0va opened this issue Jan 15, 2019 · 5 comments

Comments

@sen0va
Copy link

sen0va commented Jan 15, 2019

I tried to implement authentication with prisma with no success.
Do you have a working example with auth?

@dreglad
Copy link

dreglad commented Jan 18, 2019

It uses ApolloClient so you can set authentication as you'd normally do in Apollo. Here are some exmples.

As mentioned here, you can supply your own Apollo client (once configured for authentication)

// Create and configure your own client
const myLinkWithAuthentication = { /* see Apollo docs */ };
const client = new ApolloClient({
  link: myLinkWithAuthentication
});

// Supply the Apollo client to buildOpenCrudProvider
buildOpenCrudProvider({ client });

@MaksimKlepikov
Copy link

If you use it with a prisma token, you will receive an error message, and redirection to the login page will not occur (ra failed)
I think it is because ra trying to init but getting this error (at this point, we does't have token), so he can't redirect to login page.

Uncaught (in promise) Error: GraphQL error: Your token is invalid. It might have expired or you might be using a token from a different project.
    at new ApolloError (ApolloError.js:33)
    at eval (QueryManager.js:360)
    at eval (QueryManager.js:808)
    at Array.forEach (<anonymous>)
    at eval (QueryManager.js:807)
    at Map.forEach (<anonymous>)
    at QueryManager.broadcastQueries (QueryManager.js:803)
    at Object.next (QueryManager.js:887)
    at notifySubscription (Observable.js:152)
    at onNotify (Observable.js:196)
    at SubscriptionObserver.next (Observable.js:248)
    at eval (bundle.esm.js:98)
    at Array.forEach (<anonymous>)
    at Object.next (bundle.esm.js:97)
    at notifySubscription (Observable.js:152)
    at onNotify (Observable.js:196)
    at SubscriptionObserver.next (Observable.js:248)
    at notifySubscription (Observable.js:152)
    at onNotify (Observable.js:196)
    at SubscriptionObserver.next (Observable.js:248)
    at eval (bundle.esm.js:175)

My App.js

const authLink = setContext((_, { headers }) => {
    // get the authentication token from local storage if it exists
    const token = localStorage.getItem('token');
    // return the headers to the context so httpLink can read them
    return {
      headers: {
        ...headers,
        authorization: token ? `Bearer ${token}` : "",
      }
    }
  });
const client = new ApolloClient({
    link: authLink.concat(httpLink),
    cache: new InMemoryCache()
  });

My authProvider.js

import { AUTH_LOGIN, AUTH_LOGOUT, AUTH_ERROR, AUTH_GET_PERMISSIONS, AUTH_CHECK } from 'react-admin';
// import decodeJwt from 'jwt-decode';

export default (type, params) => {
    if (type === AUTH_LOGIN) {
        const { token } = params;
        localStorage.setItem('token', token);
    }
    if (type === AUTH_LOGOUT) {
        localStorage.removeItem('role');
        return Promise.resolve();
    }
    if (type === AUTH_ERROR) {
        debugger
    }
    if (type === AUTH_CHECK) {
        return localStorage.getItem('token') ? Promise.resolve() : Promise.reject();
    }
    return Promise.reject('Unknown method');
};

(I also use a custom login page where I just get the token from user)

How we can resolve this issue?

@gabrieltong
Copy link

+1

@Vadorequest
Copy link

Doc should be improved to showcase authentication example, it's not that straightforward.

@Vadorequest
Copy link

Vadorequest commented Mar 4, 2020

Here's how I did it.

const httpLink = createHttpLink({
      uri: 'https://api-euwest.graphcms.com/v1/xxxxx/master',
    });

    const authLink = setContext((_, { headers }) => {
      // get the authentication token from local storage if it exists
      const token = '';
      // return the headers to the context so httpLink can read them
      return {
        headers: {
          ...headers,
          authorization: token ? `Bearer ${token}` : '',
        },
      };
    });
    const client = new ApolloClient({
      link: authLink.concat(httpLink),
      cache: new InMemoryCache(),
    });

    buildGraphQLProvider({
      client,
    }).then((dataProvider) => this.setState({ dataProvider }));

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

5 participants