NextAuth not working behind corporate proxy
See original GitHub issueDescription 🐜
NextAuth is not working behind corporate proxy.
I studied the current NextAuth implementation and found that this error happens because NextAuth makes use of the “node-auth” library (npm package “oauth”) in it’s “oAuthClient” (src/server/lib/oauth/client.js), which requires manually setting up an agent to be used by the “https” library.
I created a PR with the solution of the problem: https://github.com/nextauthjs/next-auth/pull/2493
Related discussion: https://github.com/nextauthjs/next-auth/discussions/676 Related StackOverflow: https://stackoverflow.com/questions/32130471/node-js-https-not-working-behind-corporate-proxy
If the PR is not going to be accepted, i believe it is important do register the issue here for other people facing the same problems that i’m facing right now (which are quite time consuming to debug).
Is this a bug in your own project?
No
How to reproduce ☕️
Just run any application with NextAuth behind a corporate proxy and try to log in with a Provider (like Google or GitHub).
Screenshots / Logs 📽
[next-auth][error][oauth_get_access_token_error]
https://next-auth.js.org/errors#oauth_get_access_token_error Error: connect ETIMEDOUT 216.58.202.141:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '216.58.202.141',
port: 443
} undefined undefined
[next-auth][error][oauth_get_access_token_error]
https://next-auth.js.org/errors#oauth_get_access_token_error Error: connect ETIMEDOUT 216.58.202.141:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '216.58.202.141',
port: 443
} google 4/0AX4XfWiDMdHUjXWR1xqLs8bGa1g5CEJOdBiypyNPA3b4vzJHeqxOGFownSkn5ABA885Asw
[next-auth][error][oauth_callback_error]
https://next-auth.js.org/errors#oauth_callback_error Error: connect ETIMEDOUT 216.58.202.141:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '216.58.202.141',
port: 443
}
Environment 🖥
System:
OS: Windows 10 10.0.19042
CPU: (12) x64 Intel® Core™ i7-8700 CPU @ 3.20GHz
Memory: 6.80 GB / 15.78 GB
Binaries:
Node: 14.16.1 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.5 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.14.12 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: 44.19041.423.0
Internet Explorer: 11.0.19041.1
npmPackages:
react: ^17.0.2 => 17.0.2
Contributing 🙌🏽
Yes, I am willing to help solve this bug in a PR
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (6 by maintainers)
Thanks for the tip.
I have now been able to patch NextAuth v4 to be able to use it behind corporate proxy.
This is what i did:
https://github.com/nextauthjs/next-auth/blob/68e412b063e824685529bddc1288ec88d41afe4b/packages/next-auth/src/core/lib/oauth/client.ts#L16
To do so, i first updated my Provider at […nextauth].ts:
Doing like that it unfortunately didn’t work. I got this error:
Probably because the way the “agent” is being received by the openidClient function makes it stop being a Agent-like Object.
I’m applying this patch:
It would be great if this issue could be resolved in future versions! Maybe with an option like “useProxyAgent” that, if true, the openidClient function will check that a “process.env.http_proxy” exists and, if so, sets an HttpsProxyAgent like in my patch?
Anyway, in case anyone else has the same problem as i, i hope the patch can help! 😃
Thanks!
@raphaelpc we released a beta for the new NextAuth v4 version today which uses the newer
openid-client
instead ofnode-oauth
. Unfortunately there is still no built-in support for proxying.They use
sindresorhus/got
for http requests, and you can set a custom agent so it shouldn’t be too difficult to write up a workaround like the one you detailed here in your patch-package diff.Notes:
openid-client
issue: https://github.com/panva/node-openid-client/issues/22openid-client
docs ‘customizing http requests’: https://github.com/panva/node-openid-client/tree/main/docs#customizing-http-requestsgot
docs on proxies: https://github.com/sindresorhus/got/tree/v11.8.0#proxiesThinking out loud here based off the info in the above links… but maybe here (https://github.com/nextauthjs/next-auth/blob/beta/src/server/lib/oauth/client.js), adding something along these lines?