> ## Content Index
> Fetch the complete content index at: https://catalins.tech/llms.txt
> Use this file to discover other available public pages before exploring further.

# Fix Better-Auth's getSession In TanStack Start
- URL: https://catalins.tech/better-auth-getsession-fix/
- Published: 2025-06-21T13:56:18.000Z
- Updated: 2025-11-07T17:53:48.000Z
- Author: Catalin Pit
- Tags: Better Auth, Auth, TanStack Start

During the process of adding TanStack Start to my SPA application, I came across an error with Better-Auth.

The problematic part was the server function for retrieving the logged-in user's session:

```tsx
import { createServerFn } from "@tanstack/react-start";
import { getRequest } from "@tanstack/react-start/server";
import { authClient } from "../auth-client";

export const fetchUser = createServerFn({ method: "GET" }).handler(async () => {
  const request = getRequest();

  const { data } = await authClient.getSession({
    fetchOptions: {
      headers: {
        cookie: request.headers.get("cookie") || "",
      },
    },
  });

  return data;
});

```

The above code uses the `getSession` function provided by Better-Auth to fetch the user's session information. However, the function caused an infinite loop, which caused the application to crash.

The culprit was the `baseURL` property, which was missing from my configuration.

```tsx
import { createAuthClient } from "better-auth/react";
import { inferAdditionalFields } from "better-auth/client/plugins";

export const authClient = createAuthClient({
  baseURL:
    process.env.NODE_ENV === "development"
      ? "http://localhost:5173"
      : "https://mydomain.com",
  ....
});

export const {
  ....
  getSession,
} = authClient;

export type Session = typeof authClient.$Infer.Session;
export type User = typeof authClient.$Infer.Session.user;

```

Once I added the `baseURL`, the `getSession` function, and implicitly the app, worked fine.

If you want to learn more about Better-Auth, check how to [implement authentication with Better-Auth in a monorepo application](https://catalins.tech/better-auth-with-hono-bun-typescript-react-vite/).