Skip to content

pathname of NEXTAUTH_URL is ignored in when deployed to vercel #4507

@Naddiseo

Description

@Naddiseo

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:

  1. "providers" is populated in parseProviders, which gets a "url" in init()
  2. init is called in NextAuthHandler which uses req.host
  3. And req.host is provided in NextAuthNextHandler but is constructed via detectHost
  4. detecthost will return the "x-forwarded-host" header if it detects it's in vercel
  5. Back in the init parseUrl is called on the host
  6. Since parseUrl is passed the "x-forwarded-host" header instead of the url in NEXTAUTH_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 #3649 . I think if the two approaches could be used together, that would be ideal:

  1. use the x-forward-host + the pathname from NEXTAUTH_URL
  2. Or, allow basePath to be set in the nextauth config like it is for SessionProvider

How to reproduce

  1. use a custom pathname in NEXTAUTH_URL, eg: http://custom.vercel.app/api/v1/auth
  2. have a login with google
  3. deploy to vercel
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageUnseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

    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