Y0z64
Y0z649mo ago

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
0 Replies
No replies yetBe the first to reply to this messageJoin