Prefer User-Defined Host (NEXTAUTH_URL) Over Forwarded Host in Vercel#4509
Prefer User-Defined Host (NEXTAUTH_URL) Over Forwarded Host in Vercel#4509jjorissen52 wants to merge 1 commit into
Conversation
…"forwardedHost" in Vercel environment
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
|
There is a tangent situation, which I didn't mention in my original bug report: if there really is a proxy. We have two apps that connect to the same backend api (where ///next.config.js in app1 and app2
module.exports = {
rewrites() {
return [
{source: "/api/:path*", destination: `${process.env.API_URL}/api/:path*` }
]
}
};Our architecture is like this: So for each request, we need to know which of the two apps the request came from so that next-auth will redirect back to the correct one. In this particular case, the x-forward-host header is actually very useful, except when the request comes from an oauth callback. If this PR were changed slightly to pull the |
|
@Naddiseo Just reading through it, I think the callback url in that cookie is set based on the return value of In your case, you want it to be set by |
|
@jjorissen52, I think I understand what you're saying: In a non-proxy environment the I think I may have originally misunderstood how/what the if (process.env.VERCEL) {
if (process.env.NEXTAUTH_ENV) { // http://example.com/custom/api/auth/path
const {pathname} = new URL(process.env.NEXTAUTH_ENV);
return `${forwardedHost}${pathname}`; // forwarded.com/custom/api/auth/path
}
return forwardedHost
}Given that, I do think this PR is the correct fix in that it makes the code match what the documentation says it'll do. For reference, how we're solving the multi app issue is something like: /// [...nextauth].ts
export default (req,res) =>{
const forwardedHost = req.headers['x-forwarded-host'];
const callbackUrl = req.cookies['next-auth.callback-url'];
const baseUrl = isAllowedUrl(forwardedHost)
? forwardedHost
: isAllowedUrl(callbackUrl)
? callbackUrl
: undefined;
assert(typeof baseUrl !== "undefined");
const url = new Url(baseUrl);
url.pathname = "/custom/api/auth";
process.env.NEXTAUTH_URL = url.toString();
return NextAuth(req,res, { ...options});
} |
|
Yes, you seem to understand the issue now. And that code snippet is very interesting. We hadn't considered making modifications like yours. We might be able to do something similar... |
|
@Naddiseo export default (req, res) => {
/* next-auth prioritizes x-forwarded-host over NEXTAUTH_URL
when running in Vercel https://github.com/nextauthjs/next-auth/pull/4509 .
We need that to not happen, since the actual domain from *our* nginx proxy will differ
from x-forwarded-host set by Vercel's proxy. E.g.:
1. Our Nginx listening on example.dev proxies staging.example.com
2. Vercel's Nginx listening at staging.example.com sets x-forwarded-host to staging.example.com
3. next-auth will now set redirect_uri to staging.example.com instead of example.dev
*/
if (process.env.VERCEL) {
// prefer NEXTAUTH_URL, fallback to x-forwarded-host
req.headers["x-forwarded-host"] = process.env.NEXTAUTH_URL || req.headers["x-forwarded-host"]
}
return NextAuth(req, res, options) // eslint-disable-line new-cap
}Since |
|
It looks like this issue did not receive any activity for 60 days. It will be closed in 7 days if no further activity occurs. If you think your issue is still relevant, commenting will keep it open. Thanks! |
|
To keep things tidy, we are closing this issue for now. If you think your issue is still relevant, leave a comment and we might reopen it. Thanks! |
|
Please re-open this, thanks. |
This PR affects behavior that can only be observed in Vercel deployments.
The commit reverts behavior introduced by #3649, where the user-defined environment variable
NEXTAUTH_URLis ignored in favor of the value ofx-forwarded-host.☕️ Reasoning
If a user deliberately sets a
NEXTAUTH_URLenvironment variable, it means they wanted Vercel to use this as the canonical URL. next-auth should consider this deliberately defined value first and then fallback to automatic behavior otherwise. The current behavior, which is to usex-forwarded-hostwhen available and fallback to user-definedNEXTAUTH_URL(on top of being surprising and not consistent with the documentation) breaks in multiple situations, including our current staging environment:redirect_urito staging.example.com.redirect_uri=<preview_url>which is practically guaranteed to not be whitelisted by your provider (since you can't know the auto-generated preview URL ahead of time).🧢 Checklist
Documentationconsistent with existing🎫 Affected issues
Fixes: #4507
📌 Resources