T
TanStack17mo ago
absent-sapphire

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
├── 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" });
}
},
});
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" });
}
},
});
18 Replies
other-emerald
other-emerald17mo ago
either you need to rename _authenticated.tsx to authenticated.tsx (remove the leading underscore) or rename the folder authenticated to _authenticated (add a leading underscore) the second option would result in the path to just be /dashboard (without the authenticatedpart)
absent-sapphire
absent-sapphireOP17mo ago
Thank you SOOOOOO MUCH! I've bee trying to figure out where I was going wrong for some time now. Can I ask what you personally would prefer? or no underscore or no underscore
other-emerald
other-emerald17mo ago
this is not a matter of preference, but rather how the url should look like
absent-sapphire
absent-sapphireOP17mo ago
Fair point. One thing I don't get thought. TS is complaining and only know about the routes / /login /about, but it doesn't recognize /authenticated Argument of type '"/authenticated"' is not assignable to parameter of type 'keyof FileRoutesByPath'.
other-emerald
other-emerald17mo ago
how does your file/folder structure look like now? do you use the router vite plugin? is vite running?
absent-sapphire
absent-sapphireOP17mo ago
├── 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
├── 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
other-emerald
other-emerald17mo ago
i.e. did the generator run
absent-sapphire
absent-sapphireOP17mo ago
yes and yes 🙂
other-emerald
other-emerald17mo ago
would need a reproducer, e.g. by forking one of the existing example on stackblitz or this as a git repo
absent-sapphire
absent-sapphireOP17mo ago
I think the easiest way would be to give you the repo 🙂 https://github.com/Notaduck/fitter-fullstack/tree/feature/frontend
other-emerald
other-emerald17mo ago
private repo? cannot access
absent-sapphire
absent-sapphireOP17mo ago
2 sec. It shoulde be public by now. And thank you so much for the help so far!
other-emerald
other-emerald17mo ago
where should I be seeing this error?
absent-sapphire
absent-sapphireOP17mo ago
I'm so sorry! I really don't know why there was a delay, I tried to restart the TS server in vs-code, restart the application but for some was there a delay with updating routeTree.gen.ts
other-emerald
other-emerald17mo ago
so error has vanished?
absent-sapphire
absent-sapphireOP17mo ago
Si, it just took a minute or two before routeTree.gen.ts was updated and typescript could see the routes -_"
other-emerald
other-emerald17mo ago
shouldn't take that long, but well ...
absent-sapphire
absent-sapphireOP17mo ago
Nope. and my laptop isn't THAT old, not new but not that old thoug.

Did you find this page helpful?