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

Setting Headers (Auth) #129

Open
casizzi opened this issue Apr 17, 2020 · 1 comment
Open

Setting Headers (Auth) #129

casizzi opened this issue Apr 17, 2020 · 1 comment

Comments

@casizzi
Copy link

casizzi commented Apr 17, 2020

This is not really an error, more guidance, I have been struggling with setting auth headers, do you have any example? It would be awesome to add one to the readme. Thanks in advance.

@davidsonsns
Copy link

I was looking for the same answer. So, I noticed that we can get the headers info following these instructions here: https://github.com/lfades/next-with-apollo#withapollo-api.

Knowing this I implemented the package universal-cookie. Which help me to load headers.cookie and to get the token (or whatever cookie info name). See below an example:

import withApollo from 'next-with-apollo';
import ApolloClient, { InMemoryCache } from 'apollo-boost';
import { ApolloProvider } from '@apollo/react-hooks';
import Cookies from 'universal-cookie';

export default withApollo(
  ({ initialState, headers }) => {
    const cookies = new Cookies(!process.browser ? headers.cookie : null);

    return new ApolloClient({
      uri: 'https://mysite.com/graphql',
      cache: new InMemoryCache().restore(initialState || {}),
      headers: {
            your-header-info-name: cookies.get('your-cookie-info-name')
      }
    });
  },
  {
    render: ({ Page, props }) => {
      return (
        <ApolloProvider client={props.apollo}>
          <Page {...props} />
        </ApolloProvider>
      );
    }
  }
);

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

2 participants