tRPCttRPC
Powered by
Y0z64Y
tRPC•2y ago•
1 reply
Y0z64

Is there a way to pass context or cookies to the client provider?

export default function APIProvider({
  children,
}: {
  children: React.ReactNode;
}) {
  const [queryClient] = useState(() => new QueryClient());
  const [apiClient] = useState(() =>
    api.createClient({
      /**
       * Links used to determine request flow from client to server.
       *
       * @see https://trpc.io/docs/links
       */
      links: [
        loggerLink({
          enabled: (opts) =>
            process.env.NODE_ENV === "development" ||
            (opts.direction === "down" && opts.result instanceof Error),
        }),
        httpBatchLink({
          url: `${getBaseUrl()}/api/trpc`,
          headers() {
            return {
              Authorization: `Bearer ${getCookie("sessionToken") ?? undefined}`,
            };
          },
        }),
      ],
      transformer: superjson,
    }),
  );
  return (
    <api.Provider client={apiClient} queryClient={queryClient}>
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    </api.Provider>
  );
}
export default function APIProvider({
  children,
}: {
  children: React.ReactNode;
}) {
  const [queryClient] = useState(() => new QueryClient());
  const [apiClient] = useState(() =>
    api.createClient({
      /**
       * Links used to determine request flow from client to server.
       *
       * @see https://trpc.io/docs/links
       */
      links: [
        loggerLink({
          enabled: (opts) =>
            process.env.NODE_ENV === "development" ||
            (opts.direction === "down" && opts.result instanceof Error),
        }),
        httpBatchLink({
          url: `${getBaseUrl()}/api/trpc`,
          headers() {
            return {
              Authorization: `Bearer ${getCookie("sessionToken") ?? undefined}`,
            };
          },
        }),
      ],
      transformer: superjson,
    }),
  );
  return (
    <api.Provider client={apiClient} queryClient={queryClient}>
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    </api.Provider>
  );
}


I have the authorization cookies stored in HTTP-only cookies but I need to pass them to the api provider wich is client side without exposing the cookies.
I have the session cookies which contain the authorization token in the TRPC context but I cannot find a way to pass the context from there to the client provider.

Anyone knows a way I can set this header using my cookies either with context, passing the cookie directly or any other aproach?

Thanks in advance
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Is there a way to pass context or cookies to the client provider?
Y0z64YY0z64 / ❓-help
2y ago
How to pass context to vanilla client?
TuxerTTuxer / ❓-help
3y ago
is there a way to do client-side "middleware"?
TomTTom / ❓-help
3y ago
Is there a way to pass parameters to procedure on call?
StivoSStivo / ❓-help
2y ago