TanStackT
TanStack2y ago
25 replies
moderate-tomato

Can't get protected routes to work

I'm not sure what I'm doing wrong her but I cannot seem to figure out how I can create a protected route.
I'm tryint to make a protected route /authenticated/dashboard. but no matter what does_authenticated.tsx not get triggered and redirect the client to /login ?

├── index.html
├── package.json
├── package-lock.json
├── src
│   ├── App.tsx
│   ├── assets
│   ├── components
│   ├── index.css
│   ├── main.tsx
│   ├── routes
│   │   ├── authenticated
│   │   │   └── dashboard.tsx
│   │   ├── _authenticated.tsx
│   │   ├── index.lazy.tsx
│   │   ├── login.lazy.tsx
│   │   └── __root.tsx
│   ├── routeTree.gen.ts
│   └── vite-env.d.ts




import { createFileRoute, redirect } from "@tanstack/react-router";

export const Route = createFileRoute("/_authenticated")({
  beforeLoad: async ({ context }) => {
    const { isLogged } = { isLogged: () => true };
    if (isLogged()) {
      throw redirect({ to: "/login" });
    }
  },
});
Was this page helpful?