Skip to content

fix: use process.env.URL when deploying to production #1680

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

Merged
merged 9 commits into from
Oct 25, 2022
Merged
4 changes: 2 additions & 2 deletions demos/next-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"license": "MIT",
"dependencies": {
"next": "^12.3.2-canary.43",
"next-auth": "^4.7.0",
"next-auth": "^4.15.0",
"nodemailer": "^6.6.3",
"react": "^18.0.0",
"react-dom": "^18.0.0"
Expand All @@ -45,4 +45,4 @@
"engines": {
"node": ">=16.0.0"
}
}
}
49 changes: 25 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ const plugin: NetlifyPlugin = {

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

console.log(`NextAuth package detected, setting NEXTAUTH_URL environment variable to ${nextAuthUrl}`)
config.config.env.NEXTAUTH_URL = nextAuthUrl
Expand Down
49 changes: 49 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ describe('onBuild()', () => {

afterEach(() => {
delete process.env.DEPLOY_PRIME_URL
delete process.env.URL
delete process.env.CONTEXT
})

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

test('sets the NEXTAUTH_URL to the DEPLOY_PRIME_URL when CONTEXT env variable is not \'production\'', async () => {
const mockUserDefinedSiteUrl = chance.url()
process.env.DEPLOY_PRIME_URL = mockUserDefinedSiteUrl
process.env.URL = chance.url()

// See https://docs.netlify.com/configure-builds/environment-variables/#build-metadata for all possible values
process.env.CONTEXT = 'deploy-preview'

await moveNextDist()

const initialConfig = await getRequiredServerFiles(netlifyConfig.build.publish)

initialConfig.config.env.NEXTAUTH_URL = mockUserDefinedSiteUrl
await updateRequiredServerFiles(netlifyConfig.build.publish, initialConfig)

await nextRuntime.onBuild(defaultArgs)

expect(onBuildHasRun(netlifyConfig)).toBe(true)
const config = await getRequiredServerFiles(netlifyConfig.build.publish)

expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl)
})

test('sets the NEXTAUTH_URL to the user defined site URL when CONTEXT env variable is \'production\'', async () => {
const mockUserDefinedSiteUrl = chance.url()
process.env.DEPLOY_PRIME_URL = chance.url()
process.env.URL = mockUserDefinedSiteUrl

// See https://docs.netlify.com/configure-builds/environment-variables/#build-metadata for all possible values
process.env.CONTEXT = 'production'

await moveNextDist()

const initialConfig = await getRequiredServerFiles(netlifyConfig.build.publish)

initialConfig.config.env.NEXTAUTH_URL = mockUserDefinedSiteUrl
await updateRequiredServerFiles(netlifyConfig.build.publish, initialConfig)

await nextRuntime.onBuild(defaultArgs)

expect(onBuildHasRun(netlifyConfig)).toBe(true)
const config = await getRequiredServerFiles(netlifyConfig.build.publish)

expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl)
})


test('sets the NEXTAUTH_URL specified in the netlify.toml or in the Netlify UI', async () => {
const mockSiteUrl = chance.url()
process.env.NEXTAUTH_URL = mockSiteUrl
Expand Down