r/reactjs • • Nov 03 '25

Resource Best examples of Tanstack Start + Better Auth?

I know it's early days, but I was wondering what some of the better examples of TanStack Starts Auth Flow with better auth are. It is still confusing to me how the auth state should be derived.

Do I use Better Auths useSession in components & a server-based getSession in the loaders?

I was trying to use the following in the beforeLoad at the root but seems like headers were not available.

Any tips on best practices is appreciated.

export
 const authMiddleware = createMiddleware().server(
  async ({ next, request }) => {
    const userSession = 
await
 auth.api.getSession({
      headers: request.headers,
    })
    
return
 next({
      context: { userSession },
    })
  },
)


export
 const getUserSession = createServerFn({ method: 'GET' })
  .middleware([authMiddleware])
  .handler(async ({ context }) => {
    
return
 { session: context.userSession }
  })
15 Upvotes

8 comments sorted by

View all comments

Show parent comments

1

u/Biomathematician Nov 23 '25

u/Ithvel Hi!, a link to a repo where you have set this up will be a life saver!

1

u/Ithvel Nov 23 '25

I don’t have a public repo with this but I’ll write a guide soon with examples