Skip to content
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

Custom basePath not working in Vercel #8841

Open
kldavis4 opened this issue Oct 11, 2023 · 4 comments
Open

Custom basePath not working in Vercel #8841

kldavis4 opened this issue Oct 11, 2023 · 4 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@kldavis4
Copy link

Environment

It is possible to use a custom base path for the auth handler endpoint in a local environment by setting the NEXTAUTH_URL to the full path (https://next-auth.js.org/configuration/options#nextauth_url), but when deployed to Vercel, there is no way to configure the custom base path and NEXTAUTH_URL is ignored.

Reproduction URL

https://github.com/kldavis4/nextauth-custom-path-issue

Describe the issue

In the reproduction project, I have moved the auth endpoint from /api/auth to /api/foobar/auth. I've setup a discord auth provider and specified the NEXTAUTH_URL=http://localhost:3000/api/foobar/auth. With this setup, I am able to successfully login using discoard oauth. If I deploy the project to Vercel, the login form action has the default base path, and not the one specified in the NEXTAUTH_URL.

It seems like the culprit is detectOrigin which will return the full NEXTAUTH_URL when not running in Vercel, but only return the protocol+host in Vercel. There should be a way to configure the custom base path or detectOrigin should respect the path specified in NEXTAUTH_URL regardless of whether it is in Vercel.

How to reproduce

  • Clone the reproduction repository. Install deps (pnpm i).
  • Create .env.local and populate it with env vars for an oauth provider and update the auth.ts to specify the auth provider being used.
  • Set NEXTAUTH_URL=http://localhost:3000/api/foobar/auth in .env.local.
  • Run the project locally pnpm run dev and verify it is possible to login
  • Add VERCEL=1 to .env.local and run the project again and verify that it is no longer possible to login

Expected behavior

Using a custom base path should work whether running in Vercel or not.

@kldavis4 kldavis4 added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Oct 11, 2023
@NikitaKlimuk
Copy link

Hello, were you able to solve this problem?
I'm having the same problem right now with a redirect that only works on localhost

@kldavis4
Copy link
Author

Hello, were you able to solve this problem? I'm having the same problem right now with a redirect that only works on localhost

I have not - let me know if you find a way to work around it

@NikitaKlimuk
Copy link

Hi, I was able to change the default base path api/auth to next/api/auth (the first option was occupied by my backend server). In my case I had to move nextauth config to app routing (route.ts).

NEXTAUTH_URL=http://localhost:3000/next/api/auth

Also had to make changes to the file structure (src/app/next/api/auth/[...nextauth]/route.ts)

At some point the redirect worked and everything worked.

@RonnenPodolsky
Copy link

RonnenPodolsky commented Dec 21, 2023

Can someone look into this? next auth v4
It's clear there is a problem

http://localhost:3000/api/authNext/providers switching on and off VERCEL=1,

{"google":{"id":"google","name":"Google Contacts and Calendar","type":"oauth","signinUrl":"http://localhost:3000/api/authNext/signin/google","callbackUrl":"http://localhost:3000/api/authNext/callback/google"}}

vs

{"google":{"id":"google","name":"Google Contacts and Calendar","type":"oauth","signinUrl":"https://localhost:3000/api/auth/signin/google","callbackUrl":"https://localhost:3000/api/auth/callback/google"}} (prod, despite having basePath configured as required and sending NEXTAUTH_URL as env variable in vercel)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

3 participants