79607663

Date: 2025-05-05 21:34:42
Score: 0.5
Natty:
Report link

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;

Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: jjones