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?