auth.api.getSession has wrong return type #1999

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

Originally created by @mifopen on GitHub (Sep 23, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

(await auth.api.getSession(headersWithoutCookies)).someProp

Current vs. Expected behavior

Current: NRE
Expected: return type should be nullable

What version of Better Auth are you using?

1.3.14

System info

-

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

Types

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @mifopen on GitHub (Sep 23, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce (await auth.api.getSession(headersWithoutCookies)).someProp ### Current vs. Expected behavior Current: NRE Expected: return type should be nullable ### What version of Better Auth are you using? 1.3.14 ### System info ```bash - ``` ### Which area(s) are affected? (Select all that apply) Types ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 09:19:59 -05:00
Author
Owner

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

You're right—on v1.3.14, the type definition for auth.api.getSession is not nullable, but the implementation can return null in several cases (like missing or invalid cookies), which leads to null reference errors if you access properties directly. This has been fixed in a recent PR, so future versions will have the correct nullable type and prevent this issue (PR #4812).

For now, as a workaround on v1.3.14, check for null before accessing properties on the session object, or use a type cast if you need to work around TypeScript errors. Upgrading to the next release with this fix is the best long-term solution.

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 23, 2025): <!-- Answer --> You're right—on v1.3.14, the type definition for auth.api.getSession is not nullable, but the implementation can return null in several cases (like missing or invalid cookies), which leads to null reference errors if you access properties directly. This has been fixed in a recent PR, so future versions will have the correct nullable type and prevent this issue ([PR #4812](https://github.com/better-auth/better-auth/pull/4812)). For now, as a workaround on v1.3.14, check for null before accessing properties on the session object, or use a type cast if you need to work around TypeScript errors. Upgrading to the next release with this fix is the best long-term solution. <!-- 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/1c17a191-3afd-4e2f-9aba-38dd7ba3cc82?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/1c17a191-3afd-4e2f-9aba-38dd7ba3cc82?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/1c17a191-3afd-4e2f-9aba-38dd7ba3cc82?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/1c17a191-3afd-4e2f-9aba-38dd7ba3cc82?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/1c17a191-3afd-4e2f-9aba-38dd7ba3cc82?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/1c17a191-3afd-4e2f-9aba-38dd7ba3cc82?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/1c17a191-3afd-4e2f-9aba-38dd7ba3cc82?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/4834)
Author
Owner

@himself65 commented on GitHub (Nov 12, 2025):

I think we have fixed it in the 1.4

@himself65 commented on GitHub (Nov 12, 2025): I think we have fixed it in the 1.4
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1999