bxrB
Convex Community3w ago
3 replies
bxr

betterauth token returns undefined in tanstack start

On tanstack start anyone would know why the token returned from getToken always comes back undefined? I have it wrapped in a serverfn like the docs mention
const getAuth = createServerFn({ method: 'GET' }).handler(async () => {
  return await getToken()
})
...
  beforeLoad: async (ctx) => {
    const token = await ctx.context.queryClient.ensureQueryData({
      queryKey: ["token"],
      queryFn: getAuth,
      revalidateIfStale: true,
    });

    if (token) {
      // During SSR only (the only time serverHttpClient exists),
      // set the auth token to make HTTP queries with.
      ctx.context.convexQueryClient.serverHttpClient?.setAuth(token);
    }
    return {
      isAuthenticated: !!token,
      token,
    };


I checked my network tab and the token and getsession endpoints from better auth are being hit (I checked thats the provider making the calls) and they are returning my session information and on the client I can use the auth client hooks like useSession and the return my user.

One thing to call out is that I have a monorepo so convex is set up in a different service, regardless I don't think that changes things.

convex -v: 1.31.2
"@convex-dev/better-auth": "0.10.6"
Install and configure Convex + Better Auth for TanStack Start.
Was this page helpful?