using authenticatedRoute and context
I have this authenticated route that should inject userID into the context
i can access this object in my sub-routes but user object is
User | Undefined
I'd like to mark a subset of file routes as authenticated, so they will have full User
additionaly I'd also like to move to beforeLoad, but I am not sure how to do with trpc

16 Replies
equal-jadeOP•2y ago
i suppose i can do like this
but i'd like to have more assurance that i am using this correctly, feel weird
equal-jadeOP•2y ago

equal-jadeOP•2y ago
ideally this should also be without undefined
because dashboard should be somehow under the _authenticated layout
equal-jadeOP•2y ago
https://tanstack.com/router/v1/docs/framework/react/examples/authenticated-routes-context mmm here i see i should rather use provider
React Router Authenticated Routes Context Example | TanStack Router...
An example showing how to implement Authenticated Routes Context in React Router
extended-salmon•2y ago
If you are checking for the User presence in the
beforeLoad callback (throwing if not present) and returning, then by all accouts that user should be present in your child routes of the _layout.equal-jadeOP•2y ago
i really like tanstack patterns, so if we can do without context and with my appraoch that woudl be amazing
how i mark this as child of _authenticated?
extended-salmon•2y ago
Could you confirm you are using file-based routing?
equal-jadeOP•2y ago
yes
_authenticated.tsx
dashboard.tsx
extended-salmon•2y ago
Then this should work.
equal-jadeOP•2y ago
😅
nice one
extended-salmon•2y ago
If you don't want to use the beforeLoad callback for checking auth, then you could also do this checking in the component lifecycle of the
_authenticated layout route's component.
However, at this point you'll be assuming control of this stuff since this'll then be pushed out of Router land into the realm of React.equal-jadeOP•2y ago
i prefer to put as much logic into router lifecycle as possiblle
i think it makes code really clean to consume downstream
equal-jadeOP•2y ago
yes, now it works after I moved to _authenticated directory

equal-jadeOP•2y ago
can you confirm
/_authenticated/dashboard this is correct
the actual url will be //dashboard in the browserextended-salmon•2y ago
Yes, the
_layout routes are entirely transparent to the browser.
Think of it as a way of grouping related routes together. In this case, the relation is the authenticated status.
So /_authenticated/dashboard turns into /dashboard.equal-jadeOP•2y ago
tanstack router is amazing 😄
thank for you the help