nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
23.79k stars 3.27k forks source link

NEXTAUTH_URL gets strangely overwritten in next.config.js #4767

Open osequi opened 2 years ago

osequi commented 2 years ago

Environment

System:
    OS: Linux 5.4 Ubuntu 20.04.4 LTS (Focal Fossa)
    CPU: (4) x64 Intel(R) Core(TM) i5-7Y54 CPU @ 1.20GHz
    Memory: 661.49 MB / 7.64 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 14.18.3 - ~/.nvm/versions/node/v14.18.3/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 6.14.15 - ~/.nvm/versions/node/v14.18.3/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Browsers:
    Brave Browser: 102.1.39.122
    Chromium: 103.0.5060.53
    Firefox: 101.0.1
  npmPackages:
    next: latest => 12.1.0 
    next-auth: ^4.3.1 => 4.3.1 
    react: ^17.0.2 => 17.0.2 

Reproduction URL

na

Describe the issue

I have

# env.development.local
NEXTAUTH_URL=http://localhost:3000

and

// next.config.js
module.exports = {
  env: {
    NEXTAUTH_URL: "http://example.com/overwritten-in-nextconfig/",
  },
};

When running the app I'm getting:

175557400-634f92cf-3a16-49db-bc91-7f60248bf2ce

How to reproduce

In a plain simple NextAuth app add these two files env.development.local and next.config.js with the content from the above examples.

Expected behavior

The NEXTAUTH_URL should be http://example.com/overwritten-in-nextconfig/ vs. http://localhost:3000/overwritten-in-nextconfig/

osequi commented 2 years ago

Note when adding the https://example.com/custom-route/api/auth url from https://next-auth.js.org/configuration/options#nextauth_url I'm getting the same error:

image

fonzarely commented 2 years ago

Hi @osequi , I've proposed a workaround here : https://github.com/nextauthjs/next-auth/issues/4748 Hopping it could help you,

osequi commented 2 years ago

It seems NEXTAUTH_URL can be only set in .env files: https://github.com/nextauthjs/next-auth/issues/1505#issuecomment-797660838

agrittiwari commented 2 years ago

This should only be set in environment variables, but when does next.config.js runs to overwrite it?

fonzarely commented 2 years ago

I took my inspiration from this post to manage my variables setting in next.config.js You are reading environment variables the wrong way in Next.js . This make the lint, build and run to fail if an env variable is missing.