T
TanStack2y ago
stormy-gold

Redirect for home page

I would want the / path to redirect to another route for logged in users, the approach below seems to work but I'm not sure it's the right way to do it?
const indexRoute = new Route({
getParentRoute: () => rootRoute,
path: "/",
beforeLoad: ({ buildLocation, location, navigate }) => {
if (!isUserSignedIn()) {
throw redirect({
to: loginRoute.to,
search: { redirect: location.href },
});
}
const isOnHomePage =
buildLocation({ to: indexRoute.path }).pathname === location.pathname;
if (isOnHomePage) navigate({ to: defaultLoggedInRoute.to });
},
});
const indexRoute = new Route({
getParentRoute: () => rootRoute,
path: "/",
beforeLoad: ({ buildLocation, location, navigate }) => {
if (!isUserSignedIn()) {
throw redirect({
to: loginRoute.to,
search: { redirect: location.href },
});
}
const isOnHomePage =
buildLocation({ to: indexRoute.path }).pathname === location.pathname;
if (isOnHomePage) navigate({ to: defaultLoggedInRoute.to });
},
});
2 Replies
absent-sapphire
absent-sapphire2y ago
Yeah, that works.
stormy-gold
stormy-goldOP2y ago
Thanks

Did you find this page helpful?