TanStackT
TanStack14mo ago
4 replies
foolish-indigo

Using TRPC with procedures that require auth on a server

Anyone has an example of the following:
- Route that uses TRPC procedure. It's called both in loader(saved to queryCache) and then in component.
- Procedure is protected by auth that reads cookie to check for user

The problem I have is that I can't just create
createTRPCClient
with httpBatchLink url because to access auth cookie when making calls from server I'll need to manually pass headers to second call.

In Next.js I can solve it by using a different caller in server components, probably even createCallerFactory which uses trpc directly.

However here I'm not sure how to do that. The only working solution I have is to create a server function, but that's not convenient. Is there a way to run different code on SSR and client-side, while avoiding sending SSR code to client, or some other way to solve it?

(I dont think it matters but I don't use @trpc/react-query and manage my keys manually)
Was this page helpful?