T
TanStack2y ago
fascinating-indigo

context of auth not persisting on browser refresh on route beforeLoad

This seems to be the same case as the provided example in the docs https://codesandbox.io/p/devbox/github/tanstack/router/tree/main/examples/react/authenticated-routes-context?embed=1&file=%2Fsrc%2Froutes%2F__root.tsx%3A55%2C17&theme=dark After logging in and redirecting to /dashboard, when you reload the page you get the login page one more time. I am reproducing this on my app. What is the best strategy to persist the login state in this case?
2 Replies
optimistic-gold
optimistic-gold2y ago
The authenticated routes example using the Router's context is not a fully featured authentication flow. As such, it doesn't have all the features of general auth like persistence, tokens, etc. Rather the examples shows how you can bring the bits out of your existing authentication flow and pipe it into the Router's context. For persisting it, you have client-side options like localStorage and sessionStorage, or if you server controls the auth state then http-only cookies could work well here, but all of this needs to be based on the requirements of your app... You may even decide to use OAuth2 which has its own flows. But all this auth state can be passed down to the Router's context for building protected routes.
fascinating-indigo
fascinating-indigoOP2y ago
the strategy i took is getting the saved jwt wrapped with useMemo. Persistence is very hard.. thank you

Did you find this page helpful?