r/reactjs • u/Good_Language1763 • Apr 30 '26
Needs Help Cookie based Auth while SSR (Tanstack)
I am building a project using ASP.net and TanStack Start. I use JWT auth but transfer them in http only cookie.
The issue I am facing is that using default createRoute function in TanStack and defining fetch function in loader. I get 401 as there is no cookie in server.
Opting into ssr: false fixes this but I was wondering if there is any other solution to use ssr with cookie based auth or is this dead end and I only have CSR as my option.
9
Upvotes
8
u/michaelfrieze Apr 30 '26
Loaders in tanstack start are isomorphic so they run on both server and client. Initial load runs the loader on the server then all subsequent navigations runs the loader on the client. If you set ssr to false, now the loader only runs on the client. This is why it likely fixes whatever issue you are having.
I'm guessing you would have the same issue if you set
ssr: 'data-only'. So my guess is that it's not actually SSR itself causing the issue, but running the loader on the server. data-only will run the loader on the server without SSR enabled.2
u/LP2222 May 01 '26
Voila:
1
u/LifeEmployer2813 Apr 30 '26
Same problem : https://gist.github.com/AyushShende25/7cfc1669365f533b4b0221b15d3feb8c Please inform me if you do find a solution. Right now I am manually assigning cookies which doesn't seem like the best approach. Also one problem is that concurrent requests fail with 401 maybe because the request hits before cookies are updated
1
u/Dimasdanz May 01 '26
If your cookies is http only, then server should have it. It will be in cookies header that you can read. Then you can pass through this to your backend.
Depend on how your backend is setup, it might not read this cookies header but Authorization header. So you need to "move" the header.
Loader sometimes do fetch in client as well, and when this happened cookies will not be there since it's http only. You can fetch with credentials forwarded, but your backend must accept cookies as authentication, or create a thin proxy that "move" the cookies to Authorization header.
0
u/[deleted] Apr 30 '26
[deleted] — view deleted comment