question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

authorization header

See original GitHub issue

Hi Why I can’t use localStorage in withApollo.js?

I run this code:

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

export default withApollo(
  ({ initialState }) => {
    return new ApolloClient({
      uri: 'http://localhost:8000/graphql/',
      cache: new InMemoryCache().restore(initialState || {}),
      headers: {
        authorization: localStorage.getItem("token")
      }
    });
  },
  {
    render: ({ Page, props }) => {
      return (
        <ApolloProvider client={props.apollo}>
          <Page {...props} />
        </ApolloProvider>
      );
    }
  }
);

and I see this error: ReferenceError: localStorage is not defined module.exports../lib/withApollo.js.__webpack_exports__.default.next_with_apollo__WEBPACK_IMPORTED_MODULE_1___default.render.Page.Page ./lib/withApollo.js:11

Can you help me?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
chemicalkosekcommented, Mar 14, 2020

localStorage is only client side so it’s undefined server side

0reactions
AliNazariiicommented, Jun 22, 2020

Yes, It works for SSR but I didn’t want to use my token in ApolloClient. I think (and I hope) it works in ApolloClient, too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Authorization - HTTP - MDN Web Docs - Mozilla
The HTTP Authorization request header can be used to provide credentials that authenticate a user agent with a server, allowing access to a ......
Read more >
Basic access authentication - Wikipedia
In basic HTTP authentication, a request contains a header field in the form of Authorization: Basic <credentials> , where credentials is the Base64...
Read more >
Http authorization header entity definition - Microsoft Learn
An authorization header used in HTTP request. Pattern. Various authentication header formats for example: authorization: basic ********
Read more >
HTTP headers | Authorization - GeeksforGeeks
The HTTP headers Authorization header is a request type header that used to contains the credentials information to authenticate a user ...
Read more >
Bearer Authentication - Swagger
The client must send this token in the Authorization header when making requests to protected resources: Authorization: Bearer <token>.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found