repo_description: Hey is a decentralized and permissionless social media app built with Lens Protocol 🌿
repo_topics: blockchain crypto dapp graphql hacktoberfest lens-protocol nextjs react social-media tailwindcss turborepo typescript vitest wagmi web3
code: import { ApolloLink, fromPromise, toPromise } from "@apollo/client";
  import { hydrateAuthTokens, signOut } from "@/store/persisted/useAuthStore";
  import { isTokenExpiringSoon, refreshTokens } from "./tokenManager";
  
  const authLink = new ApolloLink((operation, forward) => {
    const { accessToken, refreshToken } = hydrateAuthTokens();
  
    if (!accessToken || !refreshToken) {
      signOut();
      return forward(operation);
    }
  
    const isExpiringSoon = isTokenExpiringSoon(accessToken);
  
    if (!isExpiringSoon) {
      operation.setContext({
        headers: { "X-Access-Token": accessToken }
      });
  
      return forward(operation);
    }
  
    return fromPromise(
      refreshTokens(refreshToken)
        .then((newAccessToken) => {
          operation.setContext({
            headers: { "X-Access-Token": newAccessToken }
          });
          return toPromise(forward(operation));
        })
        .catch(() => toPromise(forward(operation)))
    );
  });
  
  export default authLink;
  