Skip to content

Hosting Next Auth behind a proxy (Cloudflare Worker) - the OAuth "redirect_uri" is incorrectly set #6526

@zlwaterfield

Description

@zlwaterfield

Question 💬

  • I have an application hosted at frontend.domain.com (fake domain for privacy reasons) on Vercel.
  • I have my website hosted on another provider at site.domain.com
  • I have my root domain running on a Cloudflare worker at domain.com that will proxy some requests to site.domain.com and some to frontend.domain.com based on the path.
  • The frontend application is using Next Auth for authentication with 3 providers (credentials, Google, LinkedIn) - credentials is working fine, the issue is with the OAuth ones (Google and LinkedIn)

My entire application is working correctly except the redirect_uri on the OAuth providers. For some reason the redirect_uri's host is always frontend.domain.com.

I've scoured previous Next Auth issues and Stackoverflow and have not been able to find a solution that works.

I've tried:

  • toggling the trustHost option
  • all combinations of NEXTAUTH_URL and NEXTAUTH_URL_INTERNAL
    • NEXTAUTH_URL=domain.com
    • NEXTAUTH_URL=domain.com and NEXTAUTH_URL_INTERNAL=frontend.domain.com
    • NEXTAUTH_URL=frontend.domain.com and NEXTAUTH_URL_INTERNAL=domain.com
  • setting the callbackUrl directly from the client
  • I've also added https://frontend.domain.com/api/auth/callback/linkedin and https://domain.com/api/auth/callback/linkedin to the "Authorized redirect URLs for your app" in LinkedIn.

No matter what I do, the redirect_uri's host doesn't change.

How to reproduce ☕️

Here is the client request to domain.com/api/auth/signin/linkedin from domain.com/auth/login
Screenshot 2023-01-27 at 12 51 26 PM
Screenshot 2023-01-27 at 12 51 29 PM

And here is the /authorization request to LinkedIn from Next Auth
Screenshot 2023-01-27 at 12 51 37 PM
Screenshot 2023-01-27 at 12 51 41 PM

I'd expect when I set NEXTAUTH_URL to domain.com that the redirect_uri's host should be domain.com.

Contributing 🙌🏽

Yes, I am willing to help answer this question in a PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionAsk how to do something or how something works

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions