Idiomatic ways to handle route-guarding
This doc led me to using router contexts to be able to pass authentication context to the router https://tanstack.com/router/v1/docs/framework/react/guide/authenticated-routes#authentication-using-react-contexthooks
However I'm running into an issue. Here's my route:
On the server, I see:
Authenticated Routes | TanStack Router React Docs
Authentication is an extremely common requirement for web applications. In this guide, we'll walk through how to use TanStack Router to build protected routes, and how to redirect users to login if th...
7 Replies
equal-aquaOP•2mo ago
My
AppWithAuth
code:
In my app.tsx
, I wrap the AppWithAuth
:
My auth store is a simple zustand store. Perhaps this router context isn't actually visible by the server? Do I need to use cookies for this (that's what my intuition is telling me, but it would be magical for the server to be aware of the client side router context)?
currently every time I navigate to a /dashboard (or any children routes), it always jumps to the /signin page, then jumps right back to the target page because of the throw redirect
adding this to the top works, zero cookies needed, but kinda feels wrong
so the check is all client side, which feels fine imhoprotestant-coral•2mo ago
where is the AuthProvider rendered?
inside the root route?
anyhow, you cannot (currently) pass react hook values into router context
equal-aquaOP•2mo ago
yes its inside the root route
equal-aquaOP•2mo ago
How should I think about this comment on the docs:
from here https://tanstack.com/router/v1/docs/framework/react/guide/authenticated-routes#authentication-using-react-contexthooks
Authenticated Routes | TanStack Router React Docs
Authentication is an extremely common requirement for web applications. In this guide, we'll walk through how to use TanStack Router to build protected routes, and how to redirect users to login if th...
equal-aquaOP•2mo ago
Oh I see, perhaps I need to put the auth context above the root router, then pass it in as context
protestant-coral•2mo ago
thats not possible with start though
equal-aquaOP•2mo ago
ya makes sense now