T
TanStack•2y ago
passive-yellow

Wondering how to setup this structure via Directory routes?

Say I got these routes:
/
/auth/login
/whatever
/
/auth/login
/whatever
I've got a beforeLoad checking my session on __root.tsx which is fine and re-directs to /auth/login. But /auth/login also has to wait on the beforeLoad from root so it won't render (it triggers a warning actually: "Tried to render a redirected route match! This is a weird circumstance, please file an issue!". Wondering how you would (if you can) implement it?
5 Replies
optimistic-gold
optimistic-gold•2y ago
can you please provide a minimal complete example? e.g. by forking one of the existing examples on stackblitz
passive-yellow
passive-yellowOP•2y ago
Yup sure thing: https://stackblitz.com/edit/tanstack-router-5hukij?file=src%2Froutes%2F__root.tsx Now to me this isn't a bug, it's working as intended - the user browses the app, isn't logged in and is re-directed to /auth/login. But I am wondering if using the routes in folder structure approach, if it is possible to not have /auth/login use that "beforeLoad" from __root. hopefully that makes sense!
Simon Baeg
StackBlitz
Router Quickstart File Based Example (forked) - StackBlitz
Run official live example code for Router Quickstart File Based, created by Tanstack on StackBlitz
optimistic-gold
optimistic-gold•2y ago
maybe you can conditionally check the route in beforeLoad and skip it?
adverse-sapphire
adverse-sapphire•2y ago
Don't check the session + redirect on the root route. Add a layout route _authenticated and check + redirect there. Then make sure all the routes that should be authenticated are nested underneath. Mine looks like this:
_root.tsx
_auth.tsx <-- check here!
_auth.index.tsx
_auth.thing.tsx
login.tsx <-- this is not under _auth and thus "public"
_root.tsx
_auth.tsx <-- check here!
_auth.index.tsx
_auth.thing.tsx
login.tsx <-- this is not under _auth and thus "public"
passive-yellow
passive-yellowOP•2y ago
Thanks I'll give your suggestions a shot, usually I would just do this with nginx but trying to avoid any other tooling 🙂

Did you find this page helpful?