Skip to content

Commit 4b08911

Browse files
fix: use process.env.URL when deploying to production (#1680)
* chore: upgrade next-auth to latest * chore: upgrade next-auth again * fix: change how the base nextAuthUrl is created * style: lint * test: add test coverage for the next-auth url logic * Apply suggestions from code review Co-authored-by: Nick Taylor <[email protected]> Co-authored-by: Nick Taylor <[email protected]>
1 parent 5e906fa commit 4b08911

File tree

4 files changed

+81
-27
lines changed

4 files changed

+81
-27
lines changed

demos/next-auth/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"license": "MIT",
2525
"dependencies": {
2626
"next": "^12.3.2-canary.43",
27-
"next-auth": "^4.7.0",
27+
"next-auth": "^4.15.0",
2828
"nodemailer": "^6.6.3",
2929
"react": "^18.0.0",
3030
"react-dom": "^18.0.0"
@@ -45,4 +45,4 @@
4545
"engines": {
4646
"node": ">=16.0.0"
4747
}
48-
}
48+
}

package-lock.json

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

packages/runtime/src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ const plugin: NetlifyPlugin = {
136136

137137
await updateRequiredServerFiles(publish, config)
138138
} else {
139-
const nextAuthUrl = `${process.env.DEPLOY_PRIME_URL}${basePath}`
139+
// Using the deploy prime url in production leads to issues because the unique deploy ID is part of the generated URL
140+
// and will not match the expected URL in the callback URL of an OAuth application.
141+
const nextAuthUrl = `${
142+
process.env.CONTEXT === 'production' ? process.env.URL : process.env.DEPLOY_PRIME_URL
143+
}${basePath}`
140144

141145
console.log(`NextAuth package detected, setting NEXTAUTH_URL environment variable to ${nextAuthUrl}`)
142146
config.config.env.NEXTAUTH_URL = nextAuthUrl

test/index.js

+49
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ describe('onBuild()', () => {
232232

233233
afterEach(() => {
234234
delete process.env.DEPLOY_PRIME_URL
235+
delete process.env.URL
236+
delete process.env.CONTEXT
235237
})
236238

237239
test('does not set NEXTAUTH_URL if value is already set', async () => {
@@ -253,6 +255,53 @@ describe('onBuild()', () => {
253255
expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl)
254256
})
255257

258+
test('sets the NEXTAUTH_URL to the DEPLOY_PRIME_URL when CONTEXT env variable is not \'production\'', async () => {
259+
const mockUserDefinedSiteUrl = chance.url()
260+
process.env.DEPLOY_PRIME_URL = mockUserDefinedSiteUrl
261+
process.env.URL = chance.url()
262+
263+
// See https://docs.netlify.com/configure-builds/environment-variables/#build-metadata for all possible values
264+
process.env.CONTEXT = 'deploy-preview'
265+
266+
await moveNextDist()
267+
268+
const initialConfig = await getRequiredServerFiles(netlifyConfig.build.publish)
269+
270+
initialConfig.config.env.NEXTAUTH_URL = mockUserDefinedSiteUrl
271+
await updateRequiredServerFiles(netlifyConfig.build.publish, initialConfig)
272+
273+
await nextRuntime.onBuild(defaultArgs)
274+
275+
expect(onBuildHasRun(netlifyConfig)).toBe(true)
276+
const config = await getRequiredServerFiles(netlifyConfig.build.publish)
277+
278+
expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl)
279+
})
280+
281+
test('sets the NEXTAUTH_URL to the user defined site URL when CONTEXT env variable is \'production\'', async () => {
282+
const mockUserDefinedSiteUrl = chance.url()
283+
process.env.DEPLOY_PRIME_URL = chance.url()
284+
process.env.URL = mockUserDefinedSiteUrl
285+
286+
// See https://docs.netlify.com/configure-builds/environment-variables/#build-metadata for all possible values
287+
process.env.CONTEXT = 'production'
288+
289+
await moveNextDist()
290+
291+
const initialConfig = await getRequiredServerFiles(netlifyConfig.build.publish)
292+
293+
initialConfig.config.env.NEXTAUTH_URL = mockUserDefinedSiteUrl
294+
await updateRequiredServerFiles(netlifyConfig.build.publish, initialConfig)
295+
296+
await nextRuntime.onBuild(defaultArgs)
297+
298+
expect(onBuildHasRun(netlifyConfig)).toBe(true)
299+
const config = await getRequiredServerFiles(netlifyConfig.build.publish)
300+
301+
expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl)
302+
})
303+
304+
256305
test('sets the NEXTAUTH_URL specified in the netlify.toml or in the Netlify UI', async () => {
257306
const mockSiteUrl = chance.url()
258307
process.env.NEXTAUTH_URL = mockSiteUrl

0 commit comments

Comments
 (0)