using better-auth to protect the entire route
I have implemented better-auth for tanstack start (https://www.better-auth.com/docs/integrations/tanstack) and I want to protect all pages in the route, how can I do that?
TanStack Start Integration | Better Auth
Integrate Better Auth with TanStack Start.
6 Replies
metropolitan-bronze•6mo ago
I do this in an _auth.tsx file in the beforeload function
in my _root.tsx:
where fetchsession:
you can probably use the better auth client instead to return the session
authClient.getSession()
fascinating-indigo•6mo ago
You can also cache it with tanstack query
unwilling-turquoiseOP•6mo ago
@bxr getSession with Auth client always returns null, even when cookie is set , not sure why
Do you have an example of this? I recently started using tanstack start
fascinating-indigo•6mo ago
Something like this (mind you I have never used better auth)
fascinating-indigo•6mo ago
The above is copied from this repo:
https://github.com/dotnize/react-tanstarter
GitHub
GitHub - dotnize/react-tanstarter: minimal TanStack Start template ...
minimal TanStack Start template with React 19, Better Auth, Drizzle ORM, shadcn/ui - dotnize/react-tanstarter
fascinating-indigo•6mo ago
All credit of course to dotnize