0

I want to pass a token from my redux store to all of my Apollo requests. I know you can forward the cookies

const networkInterface = createNetworkInterface({
  uri: '/graphql',
  opts: {
    credentials: 'same-origin',
  },
});

but I want to add it to the header.

networkInterface.use([{
  applyMiddleware(req, next) {
    if (!req.options.headers) {
      req.options.headers = {};  // Create the header object if needed.
    }
    // get the authentication token from local storage if it exists
    const token = localStorage.getItem('token');
    req.options.headers.authorization = token ? `Bearer ${token}` : null;
    next();
  }
}]);

but how do I get the token out of the store. Getting it out of localStorage is not an option, because my app is server side rendered, so I don't have acces to localStorage at all times. I need to be able to access the Redux store. How do I do this?

jhamm
  • 24,124
  • 39
  • 105
  • 179

1 Answers1

0

Maybe you can export your store and import where you setup your Networkinterface. See this answer.

Reto Aebersold
  • 16,306
  • 5
  • 55
  • 74