Incorrect return type for getSession() #572

Closed
opened 2026-03-13 07:53:28 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @ammarmbe on GitHub (Jan 19, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. create a Next.js app using npx create-next-app@latest
  2. follow the docs for for the Next.js integration

Current vs. Expected behavior

authClient.getSession() incorrectly returns an object with type

Data<{
    user: {
        id: string;
        email: string;
        emailVerified: boolean;
        name: string;
        createdAt: Date;
        updatedAt: Date;
        image?: string | null | undefined | undefined;
    };
    session: {
        id: string;
        createdAt: Date;
        updatedAt: Date;
        userId: string;
        expiresAt: Date;
        token: string;
        ipAddress?: string | null | undefined | undefined;
        userAgent?: string | null | undefined | undefined;
    };
}> | Error$1<{
    code?: string;
    message?: string;
}>

This type means either data or error will be defined, and the other will be null. This is incorrect because if no error occurres and there is no user signed in, then both error and data will be null.

What version of Better Auth are you using?

1.1.14

Provide environment information

- OS: Windows 11 build 22631.4602
- Browser: Chrome v131.0.6778.265
- Runtime: Node.js v20.18.0

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

Package

Auth config (if applicable)

import { betterAuth } from "better-auth"

export const auth = betterAuth();

Additional context

No response

Originally created by @ammarmbe on GitHub (Jan 19, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. create a Next.js app using `npx create-next-app@latest` 2. follow the docs for for the [Next.js integration](https://www.better-auth.com/docs/integrations/next) ### Current vs. Expected behavior `authClient.getSession()` incorrectly returns an object with type ```ts Data<{ user: { id: string; email: string; emailVerified: boolean; name: string; createdAt: Date; updatedAt: Date; image?: string | null | undefined | undefined; }; session: { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined | undefined; userAgent?: string | null | undefined | undefined; }; }> | Error$1<{ code?: string; message?: string; }> ``` This type means either data or error will be defined, and the other will be `null`. This is incorrect because if no error occurres and there is no user signed in, then both error and data will be `null`. ### What version of Better Auth are you using? 1.1.14 ### Provide environment information ```bash - OS: Windows 11 build 22631.4602 - Browser: Chrome v131.0.6778.265 - Runtime: Node.js v20.18.0 ``` ### Which area(s) are affected? (Select all that apply) Package ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth(); ``` ### Additional context _No response_
GiteaMirror added the stalebug labels 2026-03-13 07:53:28 -05:00
Author
Owner

@mintydev789 commented on GitHub (Feb 25, 2025):

I have a somewhat similar issue. When I use the username plugin, getSession() returns username as string | null | undefined:

Image

even though it's actually supposed to be simply string:

Image

Is this better suited for a different issue? Or am I just doing something wrong? I don't get how to get the correct type for my components where I'm passing the session object. I tried import {User} from "better-auth" as well as the inferred type from my DB schema, but neither works, because the former doesn't even have the username property, and the getSession() and useSession() return types give me that string | null | undefined type for username.

@mintydev789 commented on GitHub (Feb 25, 2025): I have a somewhat similar issue. When I use the username plugin, `getSession()` returns username as `string | null | undefined`: ![Image](https://github.com/user-attachments/assets/0fb64532-787a-4e3f-b782-572e330e110b) even though it's actually supposed to be simply `string`: ![Image](https://github.com/user-attachments/assets/b0ab56bb-2798-4795-bf42-4f52e938a76a) Is this better suited for a different issue? Or am I just doing something wrong? I don't get how to get the correct type for my components where I'm passing the session object. I tried `import {User} from "better-auth"` as well as the inferred type from my DB schema, but neither works, because the former doesn't even have the `username` property, and the `getSession()` and `useSession()` return types give me that `string | null | undefined` type for `username`.
Author
Owner

@ammarmbe commented on GitHub (Feb 25, 2025):

@dmint789 add the following to your server config:

user: {
  additionalFields: {
    username: {
        type: "string",
        required: true,
    },
  },
},

and this to your client config:

inferAdditionalFields<typeof auth>()

this basically overrides the types in better auth. here's the documentation

@ammarmbe commented on GitHub (Feb 25, 2025): @dmint789 add the following to your server config: ```ts user: { additionalFields: { username: { type: "string", required: true, }, }, }, ``` and this to your client config: ```ts inferAdditionalFields<typeof auth>() ``` this basically overrides the types in better auth. [here's the documentation](https://www.better-auth.com/docs/concepts/typescript#additional-fields)
Author
Owner

@mintydev789 commented on GitHub (Feb 26, 2025):

@ammarmbe Thanks a lot! I missed that part of the docs, and it included everything I needed

@mintydev789 commented on GitHub (Feb 26, 2025): @ammarmbe Thanks a lot! I missed that part of the docs, and it included everything I needed
Author
Owner

@SohelIslamImran commented on GitHub (Mar 7, 2025):

What about this?

Image

@SohelIslamImran commented on GitHub (Mar 7, 2025): What about this? ![Image](https://github.com/user-attachments/assets/6d0e58e1-f621-48a7-82d1-9e571af58b31)
Author
Owner

@ammarmbe commented on GitHub (Mar 7, 2025):

@SohelIslamImran hmm, what does your client config look like?

@ammarmbe commented on GitHub (Mar 7, 2025): @SohelIslamImran hmm, what does your client config look like?
Author
Owner

@SohelIslamImran commented on GitHub (Mar 7, 2025):

Okay, my bad! I found that I need to import createAuthClient from 'better-auth/react" not "better-auth/client"

@SohelIslamImran commented on GitHub (Mar 7, 2025): Okay, my bad! I found that I need to import `createAuthClient` from `'better-auth/react"` not `"better-auth/client"`
Author
Owner

@dosubot[bot] commented on GitHub (Jun 15, 2025):

Hi, @ammarmbe. I'm Dosu, and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • The issue was about the incorrect return type for the getSession() method in Better Auth when used with Next.js.
  • You suggested modifying server and client configurations using Better Auth's documentation, which resolved the issue.
  • @mintydev789 and @SohelIslamImran shared similar problems, which were resolved through documentation and correct imports.
  • Discussions highlighted a need for clearer documentation or type handling in the library.

Next Steps:

  • Please confirm if this issue is still relevant to the latest version of the better-auth repository by commenting here.
  • If no updates are provided, the issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Jun 15, 2025): Hi, @ammarmbe. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - The issue was about the incorrect return type for the `getSession()` method in Better Auth when used with Next.js. - You suggested modifying server and client configurations using Better Auth's documentation, which resolved the issue. - @mintydev789 and @SohelIslamImran shared similar problems, which were resolved through documentation and correct imports. - Discussions highlighted a need for clearer documentation or type handling in the library. **Next Steps:** - Please confirm if this issue is still relevant to the latest version of the better-auth repository by commenting here. - If no updates are provided, the issue will be automatically closed in 7 days. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#572