Skip to content

Commit 9010da3

Browse files
authored
fix: don't override user defined NEXTAUTH_URL (#1360)
* fix: don't override user defined NEXTAUTH_URL A user could have defined this in next.config.js and the existing logic within the plugin overrides it. * test: small test cleanup * refactor: address cr comments
1 parent 33d1773 commit 9010da3

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

package-lock.json

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@types/node": "^17.0.10",
5757
"@types/react": "^17.0.38",
5858
"babel-jest": "^27.2.5",
59+
"chance": "^1.1.8",
5960
"cpy": "^8.1.2",
6061
"cypress": "^9.0.0",
6162
"eslint-config-next": "^12.0.0",

plugin/src/index.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,22 @@ const plugin: NetlifyPlugin = {
7676
})
7777

7878
if (isNextAuthInstalled()) {
79-
console.log(`NextAuth package detected, setting NEXTAUTH_URL environment variable to ${process.env.URL}`)
80-
8179
const config = await getRequiredServerFiles(publish)
82-
const nextAuthUrl = `${process.env.URL}${basePath}`
83-
config.config.env.NEXTAUTH_URL = nextAuthUrl
8480

85-
await updateRequiredServerFiles(publish, config)
81+
const userDefinedNextAuthUrl = config.config.env.NEXTAUTH_URL
82+
83+
if (userDefinedNextAuthUrl) {
84+
console.log(
85+
`NextAuth package detected, NEXTAUTH_URL environment variable set by user to ${userDefinedNextAuthUrl}`,
86+
)
87+
} else {
88+
const nextAuthUrl = `${process.env.URL}${basePath}`
89+
90+
console.log(`NextAuth package detected, setting NEXTAUTH_URL environment variable to ${nextAuthUrl}`)
91+
config.config.env.NEXTAUTH_URL = nextAuthUrl
92+
93+
await updateRequiredServerFiles(publish, config)
94+
}
8695
}
8796

8897
const buildId = readFileSync(join(publish, 'BUILD_ID'), 'utf8').trim()

test/index.js

+27-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ jest.mock('../plugin/src/helpers/utils', () => {
55
}
66
})
77

8+
const Chance = require('chance')
89
const { writeJSON, unlink, existsSync, readFileSync, copy, ensureDir, readJson } = require('fs-extra')
910
const path = require('path')
1011
const process = require('process')
@@ -29,6 +30,7 @@ const { getRequiredServerFiles, updateRequiredServerFiles } = require('../plugin
2930
const { dirname } = require('path')
3031
const { getProblematicUserRewrites } = require('../plugin/src/helpers/verification')
3132

33+
const chance = new Chance()
3234
const FIXTURES_DIR = `${__dirname}/fixtures`
3335
const SAMPLE_PROJECT_DIR = `${__dirname}/../demos/default`
3436
const constants = {
@@ -221,8 +223,31 @@ describe('onBuild()', () => {
221223
})
222224
})
223225

226+
afterEach(() => {
227+
delete process.env.URL
228+
})
229+
230+
test('does not set NEXTAUTH_URL if value is already set', async () => {
231+
const mockUserDefinedSiteUrl = chance.url()
232+
process.env.URL = chance.url()
233+
234+
await moveNextDist()
235+
236+
const initialConfig = await getRequiredServerFiles(netlifyConfig.build.publish)
237+
238+
initialConfig.config.env.NEXTAUTH_URL = mockUserDefinedSiteUrl
239+
await updateRequiredServerFiles(netlifyConfig.build.publish, initialConfig)
240+
241+
await plugin.onBuild(defaultArgs)
242+
243+
expect(onBuildHasRun(netlifyConfig)).toBe(true)
244+
const config = await getRequiredServerFiles(netlifyConfig.build.publish)
245+
246+
expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl)
247+
})
248+
224249
test('sets NEXTAUTH_URL when next-auth package is detected', async () => {
225-
const mockSiteUrl = 'https://my-netlify-site.app'
250+
const mockSiteUrl = chance.url()
226251

227252
// Value represents the main address to the site and is either
228253
// a Netlify subdomain or custom domain set by the user.
@@ -237,12 +262,10 @@ describe('onBuild()', () => {
237262
const config = await getRequiredServerFiles(netlifyConfig.build.publish)
238263

239264
expect(config.config.env.NEXTAUTH_URL).toEqual(mockSiteUrl)
240-
241-
delete process.env.URL
242265
})
243266

244267
test('includes the basePath on NEXTAUTH_URL when present', async () => {
245-
const mockSiteUrl = 'https://my-netlify-site.app'
268+
const mockSiteUrl = chance.url()
246269
process.env.URL = mockSiteUrl
247270

248271
await moveNextDist()
@@ -257,8 +280,6 @@ describe('onBuild()', () => {
257280
const config = await getRequiredServerFiles(netlifyConfig.build.publish)
258281

259282
expect(config.config.env.NEXTAUTH_URL).toEqual(`${mockSiteUrl}/foo`)
260-
261-
delete process.env.URL
262283
})
263284

264285
test('skips setting NEXTAUTH_URL when next-auth package is not found', async () => {

0 commit comments

Comments
 (0)