I am having some trouble passing my jwt token into the context of my Apollo server using NextJs. Before when I have been using React and Express, I was able to pass the token in the headers like this:
const client = new ApolloClient({
request: (operation) => {
const token = localStorage.getItem('id_token');
operation.setContext({
headers: {
authorization: token ? `Bearer ${token}` : '',
},
});
},
uri: '/graphql',
});
However, now I seem to be having some trouble setting the token in the header because localStorage is not defined at the time that Next is running this code. I have found tutorials for setting up JWT with Next, Next with Apollo, and Apollo with JWT. Unfortunately I haven't been able to find very much help for the specific combination of Next, Apollo, and JWT and setting the authorization header.
Any suggestions?