next-auth
next-auth copied to clipboard
pathname of `NEXTAUTH_URL` is ignored in when deployed to vercel
Environment
System: OS: Linux 5.13 Ubuntu 21.10 21.10 (Impish Indri) CPU: (16) x64 AMD Ryzen 7 2700 Eight-Core Processor Memory: 4.38 GB / 31.30 GB Container: Yes Shell: 5.1.8 - /bin/bash Browsers: Firefox: 99.0 npmPackages: next: 12.1.x => 12.1.6 next-auth: 4.3.x => 4.3.4 react: 17.0.x => 17.0.2
Reproduction URL
Describe the issue
We have NEXTAUTH_URL
set to "https://ourdomain.com/api/v1/auth" with the modified pathname since we're versioning our api, and we're correctly setting basePath
in <SessionProvider>
. This is working in our development environment, however when deploying to Vercel, the "login with google" oauth path no longer works. I have tracked this down in next-auth:
-
"providers"
is populated inparseProviders
, which gets a"url"
ininit()
-
init
is called in NextAuthHandler which usesreq.host
- And
req.host
is provided inNextAuthNextHandler
but is constructed viadetectHost
-
detecthost
will return the "x-forwarded-host" header if it detects it's in vercel -
Back in the
init
parseUrl
is called on the host - Since
parseUrl
is passed the "x-forwarded-host" header instead of the url inNEXTAUTH_URL
it cannot parse any custom set pathname, and the signinUrl and callbackUrl in the providers call become incorrect.
This seems to have been broken by https://github.com/nextauthjs/next-auth/pull/3649 . I think if the two approaches could be used together, that would be ideal:
- use the
x-forward-host
+ the pathname fromNEXTAUTH_URL
- Or, allow
basePath
to be set in the nextauth config like it is forSessionProvider
How to reproduce
- use a custom pathname in
NEXTAUTH_URL
, eg:http://custom.vercel.app/api/v1/auth
- have a login with google
- deploy to vercel
- try logging in
The call to the /providers url will return a signinUrl
, and callbackUrl
that don't have the "/v1/" part of the url and will default to "/api/auth"
Expected behavior
The redirect url provided to the signIn
call should have the custom pathname from the NEXTAUTH_URL
We can confirm this is an issue for us as well, in 4.3.33. We use NEXTAUTH_URL
(and have since we started using next-auth over a year ago), and we need it to be prioritized, because the use of the domain assigned to the app on Vercel doesn't work for us, because our app is running behind a reverse proxy, so redirecting to the subdomain (as is mentioned and crossed out in #4431) assigned to the Vercel app is breaking Google OAuth functionality for us, because the redirect_uri
is getting set incorrectly for us.
It appears that the changes made in #3649, which appear to have superceded the use of NEXTAUTH_URL
seem to not be in alignment with the documentation for how to use/configure next-auth. We need for the use of NEXTAUTH_URL
to continue to work when it's set.
Not vercel. Localhost too: https://github.com/nextauthjs/next-auth/issues/6768
This can be solved now, without breaking current deployments, see https://github.com/nextauthjs/next-auth/issues/6763#issuecomment-1455927840