Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(converters): Ensure x-forwarded-proto is set to http when running locally #887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

sommeeeer
Copy link
Contributor

@sommeeeer sommeeeer commented May 30, 2025

Discussed in #686 over at opennextjs-cloudflare

next-auth and possibly more libraries rely on the x-forwarded-proto header to be http to work out of the box when run locally. In next start or next dev they set this to http when ran locally.

To make opennextjs-cloudflarejs preview and our node converter when running OpenNext locally behave the same we would want to set this header to http. This would make the user not have to set an AUTH_URL in those modes.

Copy link

changeset-bot bot commented May 30, 2025

🦋 Changeset detected

Latest commit: 68bb9b0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@opennextjs/aws Patch
app-pages-router Patch
app-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented May 30, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/aws@887

commit: 68bb9b0

@sommeeeer sommeeeer changed the title fix(converters): Ensure x-forwarded-proto is set to http when running… fix(converters): Ensure x-forwarded-proto is set to http when running locally May 30, 2025
@sommeeeer sommeeeer requested a review from khuezy May 30, 2025 14:58
@@ -35,6 +35,12 @@ const converter: Converter<InternalEvent, InternalResult | MiddlewareResult> = {
),
);

// This is to make libraries (e.g. next-auth) that rely on this header to work out of the box in `opennextjs-cloudflare preview`.
// See https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/lib/utils/env.ts#L95-L96
if (url.protocol.startsWith("http") && url.hostname === "localhost") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like:

if (url.hostname === "localhost") {
  headers["x-forwarded-proto"] = url.protocol;
}

Because wrangler dev --local-protocol=https

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah alright. I was trying to decide if this should go in the converter or wrapper? WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@sommeeeer sommeeeer Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is again another case where the protocol would default to https if we don't have a x-forwarded-proto header set.

@sommeeeer sommeeeer marked this pull request as draft June 2, 2025 19:22
@sommeeeer
Copy link
Contributor Author

I have to dig a bit further on this one. Turns out you might need an AUTH_URL after all.

@sommeeeer
Copy link
Contributor Author

sommeeeer commented Jun 3, 2025

Im using this repo to test out. First build the repo with pnpm next build && pnpm next start and you can see it works fine.
Username: [email protected]
Password: password

However when in wrangler dev (or running express-dev/node) for some reason the callbackUrl here will have https even when I set this in the edge converter:

// this is for the redirects to work with http
process.env.__NEXT_PRIVATE_ORIGIN = url.origin;
// this makes the signin button redirect to http
headers["x-forwarded-proto"] = "http";

Remember to change the overrides path in package.json. After you have added those things above in the edge converter in your opennextjs-aws repo and built it, go and build the next-auth-test project with pnpm install && pnpm preview -- --port 3000. If you click on the Sign in with Credentials button, you will get sent to a page with this form:
image

Notice the https and not http like in next start. Thats make this form action not work out of the box. You can set an AUTH_URL and it will work, but im trying to get this to work like with next start. Still digging to figure out whats causing this.

@sommeeeer
Copy link
Contributor Author

sommeeeer commented Jun 3, 2025

Alright, this one was a bit harder to debug than I initially thought.

Turns out this is related to request.url being wrong in API routes when running OpenNext locally. The url is https.... even though you are running locally with http.

next-auth uses this url from the request object in its API handlers on /app/api/auth/[...nextauth]/route.ts.

There is some workarounds (AUTH_URL, or creating your custom signin page), but im not sure whats causing this yet, or if its even possible to fix. I checked and the req.url is correct here:

await requestHandler(requestMetadata)(req, res);

To see this yourself you can just add an API route in your app and run OpenNext locally either via opennextjs-cloudflare preview or node .open-next/server-functions/default/index.mjs:

import { NextRequest } from "next/server";

export async function GET(request: NextRequest) {
  return Response.json({
    // when running OpenNext locally this url with https when it should not
    url: request.url,
  })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants