TanStackT
TanStack3y ago
2 replies
colossal-harlequin

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 });
  },
});
Was this page helpful?