Replies: 3 comments 12 replies
-
The Kitchen Sink Authenticated file should have everything you need: Specifically this file: Your route config may look something like this:
Lastly implement your auth. You can take some inspiration from the Kitchen sink example |
Beta Was this translation helpful? Give feedback.
-
I liked the example in the Kitchen Sink, but I didn't want my protected routes to even load at all if the user was not authenticated. Here is what I implemented to accomplish this:
You would put this on the { beforeLoad } option for your protected route. Works like a charm for me. BONUS - use react query for performance
|
Beta Was this translation helpful? Give feedback.
-
For anyone using beta version or later, the Kitchen Sink example has been updated to newer APIs. TL;DR; const authenticatedRoute = new Route({
getParentRoute: () => rootRoute,
path: 'authenticated',
component: function Auth() {
const auth = useAuth()
return auth.status === 'loggedIn' ? (
<Outlet />
) : (
<div>
<div>You must log in!</div>
</div>
)
},
})
export const authenticatedIndexRoute = new Route({
getParentRoute: () => authenticatedRoute,
path: '/',
component: function Authenticated() {
return (
<div>
You're authenticated!
</div>
)
},
})
const routeTree = rootRoute.addChildren([
publicRoute,
authenticatedRoute.addChildren([authenticatedIndexRoute]),
]) |
Beta Was this translation helpful? Give feedback.
-
Hello,
thank you for this awesome router and type safety.
I'm trying to build a SPA which has only protected routes. My goal is to have routes like
/login
and/register
or the protected routes like/dashboard
,/books
, ... . My goal is after loading to check if the user is logged in (check session storage for a jwt token, then call an API endpoint to validate the session). If the session is not valid, redirect from/
to/login
(on the login page is a link to register). Then if the user logs in, the login and register route is not accessible and only the/dashboard
, ... routes. But I'm currently a bit flooded from that new technology and have no idea how to implement this. I would be happy about hints for this use case.Beta Was this translation helpful? Give feedback.
All reactions