OAuth login timeout when exchanging access_token #1887

Closed
opened 2026-03-13 09:10:54 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @luozhouyang on GitHub (Sep 9, 2025).

I encountered a problem during the OAuth login process:

2025-09-09T02:35:13.110Z ERROR [Better Auth]:  TypeError: fetch failed
    at async handleRequest (src/app/api/auth/[...all]/route.ts:9:15)
   7 |         const { env } = getCloudflareContext();
   8 |         const betterAuth = auth(env);
>  9 |         return await betterAuth.handler(req);
     |               ^
  10 |     } catch (error) {
  11 |         console.error('Auth handler error:', error);
  12 |          {
  [cause]: [Error [ConnectTimeoutError]: Connect Timeout Error (attempted address: github.com:443, timeout: 10000ms)] {
    code: 'UND_ERR_CONNECT_TIMEOUT'
  }
}
 GET /api/auth/callback/github?code=30a2b56cecc6e471574a&state=zB8MMCX5KcOa9ppj6wmynXeCysChGjof 302 in 10942ms
 GET /api/auth/error?error=invalid_code 200 in 13ms

I realized that this was because of a timeout during the OAuth process when exchanging the authorization_code for the access_token. However, I don't see how to set the timeout.

So I looked at the source code of betterAuth and found that the problem is here: packages/better-auth/src/oauth2/validate-authorization-code.ts#L109
The default betterFetch configuration is used here, and an HTTP request is sent without providing any custom options.

Normally, there is no problem with the default configuration. However, in some special cases, such as the poor network conditions I faced above, timeout problems may occur. So I think it would be best to make the parameters of betterFetch configurable.

Originally created by @luozhouyang on GitHub (Sep 9, 2025). I encountered a problem during the OAuth login process: ```bash 2025-09-09T02:35:13.110Z ERROR [Better Auth]: TypeError: fetch failed at async handleRequest (src/app/api/auth/[...all]/route.ts:9:15) 7 | const { env } = getCloudflareContext(); 8 | const betterAuth = auth(env); > 9 | return await betterAuth.handler(req); | ^ 10 | } catch (error) { 11 | console.error('Auth handler error:', error); 12 | { [cause]: [Error [ConnectTimeoutError]: Connect Timeout Error (attempted address: github.com:443, timeout: 10000ms)] { code: 'UND_ERR_CONNECT_TIMEOUT' } } GET /api/auth/callback/github?code=30a2b56cecc6e471574a&state=zB8MMCX5KcOa9ppj6wmynXeCysChGjof 302 in 10942ms GET /api/auth/error?error=invalid_code 200 in 13ms ``` I realized that this was because of a timeout during the OAuth process when exchanging the authorization_code for the access_token. However, I don't see how to set the timeout. So I looked at the source code of betterAuth and found that the problem is here: [packages/better-auth/src/oauth2/validate-authorization-code.ts#L109](https://github.com/better-auth/better-auth/blob/abfc48d2aaf1f186621c33f07bc37add1fd10e86/packages/better-auth/src/oauth2/validate-authorization-code.ts#L109) The default betterFetch configuration is used here, and an HTTP request is sent without providing any custom options. Normally, there is no problem with the default configuration. However, in some special cases, such as the poor network conditions I faced above, timeout problems may occur. So I think it would be best to make the parameters of betterFetch configurable.
Author
Owner

@dosubot[bot] commented on GitHub (Dec 9, 2025):

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

Issue Summary:

  • You reported a timeout error during OAuth login due to betterFetch's default timeout in betterAuth.
  • The timeout occurs when exchanging the authorization code for an access token.
  • You suggested making betterFetch's timeout parameters configurable to better handle poor network conditions.
  • The issue specifically references the validate-authorization-code.ts file where betterFetch is called without custom timeout options.
  • There has been no further activity or comments on this issue.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of better-auth by commenting here.
  • If I don’t hear back from you within 7 days, this issue will be automatically closed.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Dec 9, 2025): Hi, @luozhouyang. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You reported a timeout error during OAuth login due to betterFetch's default timeout in betterAuth. - The timeout occurs when exchanging the authorization code for an access token. - You suggested making betterFetch's timeout parameters configurable to better handle poor network conditions. - The issue specifically references the validate-authorization-code.ts file where betterFetch is called without custom timeout options. - There has been no further activity or comments on this issue. **Next Steps:** - Please let me know if this issue is still relevant with the latest version of better-auth by commenting here. - If I don’t hear back from you within 7 days, this issue will be automatically closed. 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#1887