signIn hangs indefinitely (Express v5) #1473

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

Originally created by @piscopancer on GitHub (Jul 7, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Current vs. Expected behavior

tldr

Attempting to call signIn does not work as the Express server never responds, after specifying a timeout for the client the response is basically nothing useful

Details

Context

I have a Turborepo that I develop around a "Studio" react component, that can be integrated into other apps and it's the part using Better Auth via an Express server

https://github.com/user-attachments/assets/5827c5e9-281e-422e-935b-ecf747de8031

In the video you see me hitting the auth button in the Studio component

<button
  onClick={async () => {
    const s = await auth.signIn.social({
      provider: 'github',
    })
    console.log(s)
  }}
>
  auth with github
</button>

that uses client auth config

import { createAuthClient } from 'better-auth/react'

export const auth = createAuthClient({
  baseURL: 'http://localhost:8484',
  fetchOptions: {
    timeout: 2000,
  },
})

and hits my Express server

import { auth, expressAdapter } from '@repo/trpc'
import { toNodeHandler } from 'better-auth/node'
import bodyParser from 'body-parser'
import cors from 'cors'
import express from 'express'

const server = express()

server.use(
  cors({
    origin: 'http://localhost:3000',
    methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
    credentials: true,
  })
)
server.use(bodyParser.json())
server.use(express.json())
server.use('/trpc', expressAdapter)

server.all('/api/auth/*splat', toNodeHandler(auth))

server.listen(8484)

which in turn uses server auth config

import { betterAuth } from 'better-auth'

export const auth = betterAuth({
  advanced: {
    cookiePrefix: 'jalyk',
    useSecureCookies: true,
  },
  baseURL: 'http://localhost:8484',
  socialProviders: {
    github: {
      clientId: 'Ov23liO...',
      clientSecret: '39098fd7...',
    },
  },
})

in the end, the fetch, unforunately, hangs forever if i do not specify the timeout, so i cannot even tell you what the error says. Pls ask if you need more info from me

What version of Better Auth are you using?

1.2.12

Provide environment information

- OS: Windows 10
- Browser: Zen

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

Backend

Auth config (if applicable)

import { betterAuth } from 'better-auth'

export const auth = betterAuth({
  advanced: {
    cookiePrefix: 'jalyk',
    useSecureCookies: true,
  },
  baseURL: 'http://localhost:8484',
  socialProviders: {
    github: {
      clientId: 'Ov23l...',
      clientSecret: '39098fd770...',
    },
  },
})

Additional context

No response

Originally created by @piscopancer on GitHub (Jul 7, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce - ### Current vs. Expected behavior # tldr Attempting to call `signIn` does not work as the Express server never responds, after specifying a timeout for the client the response is basically nothing useful # Details ## Context I have a Turborepo that I develop around a "Studio" react component, that can be integrated into other apps and it's the part using Better Auth via an Express server https://github.com/user-attachments/assets/5827c5e9-281e-422e-935b-ecf747de8031 In the video you see me hitting the auth button in the Studio component ```tsx <button onClick={async () => { const s = await auth.signIn.social({ provider: 'github', }) console.log(s) }} > auth with github </button> ``` that uses client auth config ```ts import { createAuthClient } from 'better-auth/react' export const auth = createAuthClient({ baseURL: 'http://localhost:8484', fetchOptions: { timeout: 2000, }, }) ``` and hits my Express server ```ts import { auth, expressAdapter } from '@repo/trpc' import { toNodeHandler } from 'better-auth/node' import bodyParser from 'body-parser' import cors from 'cors' import express from 'express' const server = express() server.use( cors({ origin: 'http://localhost:3000', methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], credentials: true, }) ) server.use(bodyParser.json()) server.use(express.json()) server.use('/trpc', expressAdapter) server.all('/api/auth/*splat', toNodeHandler(auth)) server.listen(8484) ``` which in turn uses server auth config ```ts import { betterAuth } from 'better-auth' export const auth = betterAuth({ advanced: { cookiePrefix: 'jalyk', useSecureCookies: true, }, baseURL: 'http://localhost:8484', socialProviders: { github: { clientId: 'Ov23liO...', clientSecret: '39098fd7...', }, }, }) ``` in the end, the fetch, unforunately, hangs forever if i do not specify the timeout, so i cannot even tell you what the error says. Pls ask if you need more info from me ### What version of Better Auth are you using? 1.2.12 ### Provide environment information ```bash - OS: Windows 10 - Browser: Zen ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from 'better-auth' export const auth = betterAuth({ advanced: { cookiePrefix: 'jalyk', useSecureCookies: true, }, baseURL: 'http://localhost:8484', socialProviders: { github: { clientId: 'Ov23l...', clientSecret: '39098fd770...', }, }, }) ``` ### Additional context _No response_
GiteaMirror added the express label 2026-03-13 08:42:09 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 7, 2025):

Hi @piscopancer! I'm Dosu and I’m helping the better-auth team.

This is a known pitfall with Better Auth and Express v5. There are a few things in your setup that can cause requests to hang indefinitely:

  • Using express.json() or bodyParser.json() before the Better Auth handler can break request processing and cause endpoints to hang or return NOT_FOUND. Remove both of these middlewares before mounting toNodeHandler(auth) reference.
  • For Express v5, the catch-all route syntax has changed. You should use:
    app.all('/api/auth/{*any}', toNodeHandler(auth));
    
    instead of the v4-style * or :splat reference.
  • Make sure the baseURL in your Better Auth config exactly matches the path where you mount the handler in Express, including any prefixes. If they don’t match, you’ll get NOT_FOUND errors and hanging requests reference.

Try removing the JSON body parsers before the auth handler, update your route to use the new Express v5 catch-all syntax, and double-check that your baseURL matches the mount path. If it still hangs, add a simple log statement before and inside the handler to confirm the route is being hit.

There’s also a disclaimer in the docs about Express v5 support, so if you continue to see issues, testing with Express v4 can help confirm if it’s a v5-specific problem reference.

To reply, just mention @dosu.


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

@dosubot[bot] commented on GitHub (Jul 7, 2025): <!-- Greeting --> Hi @piscopancer! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known pitfall with Better Auth and Express v5. There are a few things in your setup that can cause requests to hang indefinitely: - Using `express.json()` or `bodyParser.json()` before the Better Auth handler can break request processing and cause endpoints to hang or return NOT_FOUND. Remove both of these middlewares before mounting `toNodeHandler(auth)` [reference](https://github.com/better-auth/better-auth/issues/2975). - For Express v5, the catch-all route syntax has changed. You should use: ```js app.all('/api/auth/{*any}', toNodeHandler(auth)); ``` instead of the v4-style `*` or `:splat` [reference](https://github.com/better-auth/better-auth/issues/2975). - Make sure the `baseURL` in your Better Auth config exactly matches the path where you mount the handler in Express, including any prefixes. If they don’t match, you’ll get NOT_FOUND errors and hanging requests [reference](https://github.com/better-auth/better-auth/issues/3215). Try removing the JSON body parsers before the auth handler, update your route to use the new Express v5 catch-all syntax, and double-check that your `baseURL` matches the mount path. If it still hangs, add a simple log statement before and inside the handler to confirm the route is being hit. There’s also a disclaimer in the docs about Express v5 support, so if you continue to see issues, testing with Express v4 can help confirm if it’s a v5-specific problem [reference](https://github.com/better-auth/better-auth/pull/2433). <!-- 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/758698f1-a62c-4497-b165-64f1d3ce02ad?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/758698f1-a62c-4497-b165-64f1d3ce02ad?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/758698f1-a62c-4497-b165-64f1d3ce02ad?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/758698f1-a62c-4497-b165-64f1d3ce02ad?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/758698f1-a62c-4497-b165-64f1d3ce02ad?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/758698f1-a62c-4497-b165-64f1d3ce02ad?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/758698f1-a62c-4497-b165-64f1d3ce02ad?feedback_type=other)</sup>&nbsp;&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/3295)
Author
Owner

@piscopancer commented on GitHub (Jul 7, 2025):

the 1st option suggested by @dosubot helped

Avoid using express.json() before the Better Auth handler, as it can interfere with request processing and cause endpoints to hang or return NOT_FOUND responses

@piscopancer commented on GitHub (Jul 7, 2025): the 1st option suggested by @dosubot helped > Avoid using express.json() before the Better Auth handler, as it can interfere with request processing and cause endpoints to hang or return NOT_FOUND responses
Author
Owner

@dosubot[bot] commented on GitHub (Jul 7, 2025):

Great to hear that solved it! For future reference, using express.json() (or bodyParser.json()) before the Better Auth handler can cause issues because those middlewares consume the request body, which Better Auth needs to process authentication flows. This can lead to requests hanging or returning NOT_FOUND responses. If you run into any more questions about Better Auth or Express v5 setup, just let me know!

To reply, just mention @dosu.


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

@dosubot[bot] commented on GitHub (Jul 7, 2025): <!-- Answer --> Great to hear that solved it! For future reference, using express.json() (or bodyParser.json()) before the Better Auth handler can cause issues because those middlewares consume the request body, which Better Auth needs to process authentication flows. This can lead to requests hanging or returning NOT_FOUND responses. If you run into any more questions about Better Auth or Express v5 setup, just let me know! <!-- 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/528386ee-d495-4d88-896a-0e5c29ac5169?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/528386ee-d495-4d88-896a-0e5c29ac5169?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/528386ee-d495-4d88-896a-0e5c29ac5169?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/528386ee-d495-4d88-896a-0e5c29ac5169?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/528386ee-d495-4d88-896a-0e5c29ac5169?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/528386ee-d495-4d88-896a-0e5c29ac5169?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/528386ee-d495-4d88-896a-0e5c29ac5169?feedback_type=other)</sup>&nbsp;&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/3295)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1473