T
TanStack9mo ago
like-gold

Authentication

Hello everyone How I can handle authentication using Tanstack Router? Is possible to redirect to the login page when axios return a status code 401 and redirect to dashboard when access login and already have a httponly cookie with authentication token?
3 Replies
optimistic-gold
optimistic-gold9mo ago
You should be able to do it in the beforeLoad function Do it at the top wrapper of Authenticated Routes and it would run for all child routes (as far I know) And there is a throw redirect approach to redirect to login page from there Check https://tanstack.com/router/v1/docs/framework/react/guide/authenticated-routes
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...
optimistic-gold
optimistic-gold9mo ago
For axios probably can wire up an interceptor to do same when it’s 401 response
like-gold
like-goldOP9mo ago
I'm a little bit confuse, but thanks I need to search a little bit
Do it at the top wrapper of Authenticated Routes and it would run for all child routes (as far I know)
I didn't create child routes :/ should I do it? I create only two separated layouts

Did you find this page helpful?