Host name error on NextJS api route

I am trying to deploy a NextJS app using the next-on-netlify plugin. I am using Auth0 for authentication and when my app goes to the api route to login (/api/login) I get an error that says The "host" request header is not available.

I am guessing it’s because the API is redirecting to Auth0 to show their Login page. Do I need some custom headers config? Anyone else seen this and resolved it?

Thanks

Hard to say exactly what is happening since that is not a Netlify error message (must be either in your code or from the remote service). Could you describe the path your request takes in higher detail? I’m looking for something like this:

  1. request comes in from browser, POST to /api/login
  2. that is proxy’d to a netlify function at path /.netlify/functions/auth0
  3. that function contacts auth0, and that error message comes from their service

Once we know that, we’ll be able to hopefully give some better advice :slight_smile:

Thanks for the help. I found that the place where my function is getting the error. See below

Looks like my API route is trying to create a cookie before it redirects to Auth0. For some reason though req.headers.host does not have a value when it’s being processed in the Netlify function.

I am using the Next-on-Netlify lib for this.

Thanks

Gus

Hi @gushiguera,

I’m not familiar with your code but the only time I can think of where a request won’t have a host header from your browser would be if it was an OPTION request (where it’s not a GET or a POST). If a request is initiated by a website, the browser will first send an OPTION request before the ‘real’ request. Perhaps you’ll need to make sure you handle OPTION requests explicitly. Hope that helps.

I’m setting up a NextJS project with next-on-netlify using Auth0 following available documentation as well and I’m getting the same error. As everything works just fine on localhost, I suspect this is an issue with how Netlify and Auth0 play together, and neither party has documented the appropriate way to handle the integration.

I’m pretty unfamiliar with CORS and request headers in general so @gushiguera if you happened to figure out the correct way forward I would love to know! I’ll start digging in, myself.

I couldn’t figure it out and actually gave up on it. Using AWS and serverless to deploy my site. 100% easier. Wish it was easier on Netlify.

1 Like

That’s disappointing, I really wanted to like Netlify! Thanks for the heads-up, I’ll switch over to AWS :slight_smile:

Hi all! I have a PR on our end to make sure that host header is available when using next-on-netlify, so the issue should be resolved at its source once that PR is deployed.

In the meantime, there’s another PR on next-on-netlify to work around the issue here: https://github.com/netlify/next-on-netlify/pull/44, which is already merged and should be available in version 2.5.0 and above.