Thank you for this post. I just wanted to add more context. When you log in with mutation AuthenticateUserWithPassword
it will return sessionToken
. Make sure you pass the sessionToken token as a Bearer token and authenticatedItem
item query will return the correct data.
import {
ApolloClient,
InMemoryCache,
HttpLink
} from "@apollo/client";
import {
setContext
} from "@apollo/client/link/context";
const httpLink = new HttpLink({
uri: "http://localhost:3000/api/graphql",
});
const authLink = setContext((_, {
headers
}) => {
// Read the sessionToken from localStorage
const token = localStorage.getItem("sessionToken");
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
},
};
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
});
export default client;