regression: auth.api.getSession()'s return type is never null #1984

Closed
opened 2026-03-13 09:19:00 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @firatciftci on GitHub (Sep 20, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Using Better Auth version 1.3.13, set up a new application
  2. Access the session on the server side using auth.api.getSession()
  3. Check if the returned session value is ever null (which should be the case when the user is not logged in)
  4. (If you have type-aware ESLint enabled, see the following error: Unnecessary conditional, the types have no overlap.)

Current vs. Expected behavior

Type type of the returned value from auth.api.getSession() should be a union of an actual value and null. It currently does not have null.

What version of Better Auth are you using?

1.3.13

System info

{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 25.0.0: Mon Aug 25 21:17:54 PDT 2025; root:xnu-12377.1.9~3/RELEASE_ARM64_T6041",
    "release": "25.0.0",
    "cpuCount": 14,
    "cpuModel": "Apple M4 Pro",
    "totalMemory": "24.00 GB",
    "freeMemory": "0.17 GB"
  },
  "node": {
    "version": "v22.19.0",
    "env": "development"
  },
  "packageManager": {
    "name": "pnpm",
    "version": "10.17.0"
  },
  "frameworks": [
    {
      "name": "svelte",
      "version": "^5.39.3"
    },
    {
      "name": "@sveltejs/kit",
      "version": "^2.42.2"
    }
  ],
  "databases": [
    {
      "name": "mongodb",
      "version": "^6.20.0"
    }
  ],
  "betterAuth": {
    "version": "^1.3.13",
    "config": {
      "plugins": [
        {
          "name": "sveltekit-cookies",
          "config": {
            "id": "sveltekit-cookies",
            "hooks": {
              "after": [
                {}
              ]
            }
          }
        }
      ],
      "secret": "[REDACTED]",
      "trustedOrigins": [
        "http://localhost:5173"
      ],
      "emailAndPassword": {
        "enabled": true
      }
    }
  }
}

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import { connect, getDB } from "./db-connect";
import { sveltekitCookies } from "better-auth/svelte-kit";
import { getRequestEvent } from "$app/server";
import { BETTER_AUTH_SECRET, BETTER_AUTH_URL } from "$env/static/private";

await connect();

const db = getDB();

export const auth = betterAuth({
  database: mongodbAdapter(db, { usePlural: true }),
  plugins: [sveltekitCookies(getRequestEvent)],
  secret: BETTER_AUTH_SECRET,
  trustedOrigins: [BETTER_AUTH_URL],
  emailAndPassword: {
    enabled: true,
  },
});

Additional context

I suspect the following PR introduced this regression: https://github.com/better-auth/better-auth/pull/3983

Originally created by @firatciftci on GitHub (Sep 20, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Using Better Auth version 1.3.13, set up a new application 2. Access the session on the server side using `auth.api.getSession()` 3. Check if the returned session value is ever `null` (which should be the case when the user is not logged in) 4. (If you have type-aware ESLint enabled, see the following error: `Unnecessary conditional, the types have no overlap.`) ### Current vs. Expected behavior Type type of the returned value from `auth.api.getSession()` should be a union of an actual value and `null`. It currently does not have `null`. ### What version of Better Auth are you using? 1.3.13 ### System info ```bash { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 25.0.0: Mon Aug 25 21:17:54 PDT 2025; root:xnu-12377.1.9~3/RELEASE_ARM64_T6041", "release": "25.0.0", "cpuCount": 14, "cpuModel": "Apple M4 Pro", "totalMemory": "24.00 GB", "freeMemory": "0.17 GB" }, "node": { "version": "v22.19.0", "env": "development" }, "packageManager": { "name": "pnpm", "version": "10.17.0" }, "frameworks": [ { "name": "svelte", "version": "^5.39.3" }, { "name": "@sveltejs/kit", "version": "^2.42.2" } ], "databases": [ { "name": "mongodb", "version": "^6.20.0" } ], "betterAuth": { "version": "^1.3.13", "config": { "plugins": [ { "name": "sveltekit-cookies", "config": { "id": "sveltekit-cookies", "hooks": { "after": [ {} ] } } } ], "secret": "[REDACTED]", "trustedOrigins": [ "http://localhost:5173" ], "emailAndPassword": { "enabled": true } } } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { mongodbAdapter } from "better-auth/adapters/mongodb"; import { connect, getDB } from "./db-connect"; import { sveltekitCookies } from "better-auth/svelte-kit"; import { getRequestEvent } from "$app/server"; import { BETTER_AUTH_SECRET, BETTER_AUTH_URL } from "$env/static/private"; await connect(); const db = getDB(); export const auth = betterAuth({ database: mongodbAdapter(db, { usePlural: true }), plugins: [sveltekitCookies(getRequestEvent)], secret: BETTER_AUTH_SECRET, trustedOrigins: [BETTER_AUTH_URL], emailAndPassword: { enabled: true, }, }); ``` ### Additional context I suspect the following PR introduced this regression: https://github.com/better-auth/better-auth/pull/3983
GiteaMirror added the bug label 2026-03-13 09:19:00 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 20, 2025):

You are correct—there is a regression in the TypeScript return type of auth.api.getSession() in Better Auth v1.3.13. The recent changes in PR #3983 introduced new generics and options to getSession, but the updated type definition does not explicitly include null in the return type when no session exists. This means TypeScript will not allow you to check for null, which leads to the ESLint error you described. The runtime can still return null data, but the type signature no longer reflects that possibility, which is a regression from expected behavior and could break type-safe code that handles unauthenticated sessions.

There are no type tests or documentation in that PR confirming that null is part of the return type, and the type tests only check for Response or objects with headers, not for null values in the session result. This change likely caused the issue you’re seeing.
See the relevant PR and type changes here.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Sep 20, 2025): <!-- Answer --> You are correct—there is a regression in the TypeScript return type of `auth.api.getSession()` in Better Auth v1.3.13. The recent changes in [PR #3983](https://github.com/better-auth/better-auth/pull/3983) introduced new generics and options to `getSession`, but the updated type definition does not explicitly include `null` in the return type when no session exists. This means TypeScript will not allow you to check for `null`, which leads to the ESLint error you described. The runtime can still return `null` data, but the type signature no longer reflects that possibility, which is a regression from expected behavior and could break type-safe code that handles unauthenticated sessions. There are no type tests or documentation in that PR confirming that `null` is part of the return type, and the type tests only check for `Response` or objects with headers, not for `null` values in the session result. This change likely caused the issue you’re seeing. [See the relevant PR and type changes here.](https://github.com/better-auth/better-auth/pull/3983) <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/91a3e98d-f258-45d5-be2e-6c31fac82adc?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/91a3e98d-f258-45d5-be2e-6c31fac82adc?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/91a3e98d-f258-45d5-be2e-6c31fac82adc?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/91a3e98d-f258-45d5-be2e-6c31fac82adc?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/91a3e98d-f258-45d5-be2e-6c31fac82adc?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/91a3e98d-f258-45d5-be2e-6c31fac82adc?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/91a3e98d-f258-45d5-be2e-6c31fac82adc?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/4790)
Author
Owner

@elvince commented on GitHub (Sep 22, 2025):

Hi,

I'm also impacted by this release.
Could you deliver a quick fix on this?
Thanks,

@elvince commented on GitHub (Sep 22, 2025): Hi, I'm also impacted by this release. Could you deliver a quick fix on this? Thanks,
Author
Owner

@Mateleo commented on GitHub (Sep 22, 2025):

Same issue with Nuxt SSR

@Mateleo commented on GitHub (Sep 22, 2025): Same issue with Nuxt SSR
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1984