Unauthorized Error while prefetching from protected procedure
I was working on a nextjs monorepo using trpc as backend I am integrating SSE with normal trpc methods, I implemented auth using better auth and it works very smoothly, but when I try to prefetch on server components I get not authenticated user error, but after render the app works without error. I checked further and reached upon conclusion that cookies aren't resolved because when I tried passing headers vis prop drilling from TRPC provider and using those in getting auth it worked and prefetching worked. Here is my problem, any help would be appreciated
https://stackoverflow.com/questions/79823260/trpc-prefetching-fails-on-protected-procedure
1 Reply
Yes this is fairly normal, cookies may not be sent on the first request to the server and some juggling is required to get them. I only have experience fixing this with Tanstack Start but nextjs should have a best practice way to do it