Skip to content

fix: respect variable set in Netlify config for NEXTAUTH_URL #1613

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 2 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,20 @@ const plugin: NetlifyPlugin = {

if (userDefinedNextAuthUrl) {
console.log(
`NextAuth package detected, NEXTAUTH_URL environment variable set by user to ${userDefinedNextAuthUrl}`,
`NextAuth package detected, NEXTAUTH_URL environment variable set by user in next.config.js to ${userDefinedNextAuthUrl}`,
)
} else if (process.env.NEXTAUTH_URL) {
// When the value is specified in the netlify.toml or the Netlify UI (will be evaluated in this order)
const nextAuthUrl = `${process.env.NEXTAUTH_URL}${basePath}`

console.log(
`NextAuth package detected, NEXTAUTH_URL environment variable set by user in Netlify configuration to ${nextAuthUrl}`,
)
config.config.env.NEXTAUTH_URL = nextAuthUrl

await updateRequiredServerFiles(publish, config)
} else {
const nextAuthUrl = `${process.env.URL}${basePath}`
const nextAuthUrl = `${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
23 changes: 19 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,12 @@ describe('onBuild()', () => {
})

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

test('does not set NEXTAUTH_URL if value is already set', async () => {
const mockUserDefinedSiteUrl = chance.url()
process.env.URL = chance.url()
process.env.DEPLOY_PRIME_URL = chance.url()

await moveNextDist()

Expand All @@ -252,13 +252,28 @@ describe('onBuild()', () => {
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

await moveNextDist()

await nextRuntime.onBuild(defaultArgs)

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

expect(config.config.env.NEXTAUTH_URL).toEqual(mockSiteUrl)
delete process.env.NEXTAUTH_URL
})

test('sets NEXTAUTH_URL when next-auth package is detected', async () => {
const mockSiteUrl = chance.url()

// Value represents the main address to the site and is either
// a Netlify subdomain or custom domain set by the user.
// See https://docs.netlify.com/configure-builds/environment-variables/#deploy-urls-and-metadata
process.env.URL = mockSiteUrl
process.env.DEPLOY_PRIME_URL = mockSiteUrl

await moveNextDist()

Expand All @@ -272,7 +287,7 @@ describe('onBuild()', () => {

test('includes the basePath on NEXTAUTH_URL when present', async () => {
const mockSiteUrl = chance.url()
process.env.URL = mockSiteUrl
process.env.DEPLOY_PRIME_URL = mockSiteUrl

await moveNextDist()

Expand Down