[GH-ISSUE #1335] Connection terminated due to connection timeout #8698

Closed
opened 2026-04-13 03:51:42 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @dheerajsarwaiya on GitHub (Feb 2, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1335

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I am using better auth in nextjs with drizzle and postgres. Everything works fine in the production. However, in dev, every now and then I get auth error. I have to refresh the page to get ride of the error.

2025-02-02T23:31:15.733Z ERROR [Better Auth]: INTERNAL_SERVER_ERROR Error: Connection terminated due to connection timeout
    at async getCurrentServerUser (src/lib/auth/server.ts:14:18)
    at async MainLayout (src/app/orgs/(channels)/[channelId]/layout.tsx:19:15)
  12 |
  13 | export const getCurrentServerUser = async () => {
> 14 |   const session = await auth.api.getSession({
     |                  ^
  15 |     headers: await headers(), // you need to pass the headers object.
  16 |   });
  17 |   if (!session?.user) return null;
 ⨯ [Error [BetterCallAPIError]: API Error: INTERNAL_SERVER_ERROR Failed to get session] {
  status: 'INTERNAL_SERVER_ERROR',
  headers: Headers {},
  body: [Object],
  digest: '2603914421',
  [cause]: [Object]
}

in the middleware I have this

const { data: session } = await betterFetch<Session>(
    "/api/auth/get-session",
    {
      baseURL: request.nextUrl.origin,
      headers: {
        //get the cookie from the request
        cookie: request.headers.get("cookie") || "",
      },
    }
  );

Current vs. Expected behavior

getting error

What version of Better Auth are you using?

"better-auth": "^1.1.10",

Provide environment information

OS: mac
framework: nextjs
DB: postgres with drizzle
In dev environment running postgres on Docker. In production, using supabase for postgres.

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

Backend

Auth config (if applicable)

export const auth = betterAuth({
  appName: "AppName",
  session: {
    freshAge: 0,
  },
  database: drizzleAdapter(db, {
    provider: "pg",
    schema: {
      user: usersTable,
      session: sessionsTable,
      account: accountsTable,
      verification: verificationTokensTable,
    },
  }),
  user: {
    additionalFields: {
      isOnboarded: {
        type: "boolean",
        required: false,
        defaultValue: false,
        input: false,
      },
      role: {
        type: "string",
        required: false,
        defaultValue: "user",
        input: false, // don't allow user to set role
      },
      username: {
        type: "string",
        required: false,
        defaultValue: null,
        input: true,
      },
    },
  },
  socialProviders: {
    google: {
      clientId: process.env.AUTH_GOOGLE_ID,
      clientSecret: process.env.AUTH_GOOGLE_SECRET,
    },
  },
  plugins: [
    openAPI(),
    emailOTP({
      async sendVerificationOTP({ email, otp, type }) {
        if (type === "sign-in") {
          await local_sendEmailOTPRequest({ email, otp: otp });
        }
      },
    }),
    magicLink({
      async sendMagicLink(data) {
        const { url, email } = data;
        await local_sendVerificationRequest({ email, url });
      },
    }),
  ],
});

Additional context

No response

Originally created by @dheerajsarwaiya on GitHub (Feb 2, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1335 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce I am using better auth in nextjs with drizzle and postgres. Everything works fine in the production. However, in dev, every now and then I get auth error. I have to refresh the page to get ride of the error. ``` 2025-02-02T23:31:15.733Z ERROR [Better Auth]: INTERNAL_SERVER_ERROR Error: Connection terminated due to connection timeout at async getCurrentServerUser (src/lib/auth/server.ts:14:18) at async MainLayout (src/app/orgs/(channels)/[channelId]/layout.tsx:19:15) 12 | 13 | export const getCurrentServerUser = async () => { > 14 | const session = await auth.api.getSession({ | ^ 15 | headers: await headers(), // you need to pass the headers object. 16 | }); 17 | if (!session?.user) return null; ⨯ [Error [BetterCallAPIError]: API Error: INTERNAL_SERVER_ERROR Failed to get session] { status: 'INTERNAL_SERVER_ERROR', headers: Headers {}, body: [Object], digest: '2603914421', [cause]: [Object] } ``` in the middleware I have this ``` const { data: session } = await betterFetch<Session>( "/api/auth/get-session", { baseURL: request.nextUrl.origin, headers: { //get the cookie from the request cookie: request.headers.get("cookie") || "", }, } ); ``` ### Current vs. Expected behavior getting error ### What version of Better Auth are you using? "better-auth": "^1.1.10", ### Provide environment information ```bash OS: mac framework: nextjs DB: postgres with drizzle In dev environment running postgres on Docker. In production, using supabase for postgres. ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript export const auth = betterAuth({ appName: "AppName", session: { freshAge: 0, }, database: drizzleAdapter(db, { provider: "pg", schema: { user: usersTable, session: sessionsTable, account: accountsTable, verification: verificationTokensTable, }, }), user: { additionalFields: { isOnboarded: { type: "boolean", required: false, defaultValue: false, input: false, }, role: { type: "string", required: false, defaultValue: "user", input: false, // don't allow user to set role }, username: { type: "string", required: false, defaultValue: null, input: true, }, }, }, socialProviders: { google: { clientId: process.env.AUTH_GOOGLE_ID, clientSecret: process.env.AUTH_GOOGLE_SECRET, }, }, plugins: [ openAPI(), emailOTP({ async sendVerificationOTP({ email, otp, type }) { if (type === "sign-in") { await local_sendEmailOTPRequest({ email, otp: otp }); } }, }), magicLink({ async sendMagicLink(data) { const { url, email } = data; await local_sendVerificationRequest({ email, url }); }, }), ], }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 03:51:42 -05:00
Author
Owner

@moshetanzer commented on GitHub (Mar 23, 2025):

Hey,

Unfortunately this is more likely than not an issue with your connection to db ❤

<!-- gh-comment-id:2746403305 --> @moshetanzer commented on GitHub (Mar 23, 2025): Hey, Unfortunately this is more likely than not an issue with your connection to db ❤
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8698