From e2252c01cf09a28e06ad653f7a1cb8444d1a9598 Mon Sep 17 00:00:00 2001 From: Michael Zetterberg Date: Mon, 22 Apr 2024 17:50:54 +0200 Subject: [PATCH] feat: set NEXTAUTH_URL based on DEPLOY_PRIME_URL on Netlify --- .env.local.example | 3 ++- auth.ts | 1 - env/server.ts | 2 -- netlify.toml | 12 +++++++++--- next.config.js | 13 +++++++++++++ 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/.env.local.example b/.env.local.example index c62dd0f6a..558a6cf35 100644 --- a/.env.local.example +++ b/.env.local.example @@ -12,10 +12,11 @@ CURITY_CLIENT_ID_USER="" CURITY_CLIENT_SECRET_USER="" CURITY_ISSUER_USER="https://testlogin.scandichotels.com" CYPRESS_BASE_URL="http://localhost:3000" +# See next.config.js for info +DEPLOY_PRIME_URL="http://localhost:3000" DESIGN_SYSTEM_ACCESS_TOKEN="" NEXTAUTH_REDIRECT_PROXY_URL="http://localhost:3000/api/web/auth" NEXTAUTH_SECRET="" -NEXTAUTH_URL="http://localhost:3000/" REVALIDATE_SECRET="" SEAMLESS_LOGIN_DA="http://www.example.dk/updatelogin" SEAMLESS_LOGIN_DE="http://www.example.de/updatelogin" diff --git a/auth.ts b/auth.ts index ad1dcdc81..e40ee7767 100644 --- a/auth.ts +++ b/auth.ts @@ -45,7 +45,6 @@ const customProvider = { } satisfies OIDCConfig export const config = { - basePath: "/api/web/auth", providers: [customProvider], redirectProxyUrl: env.NEXTAUTH_REDIRECT_PROXY_URL, trustHost: true, diff --git a/env/server.ts b/env/server.ts index 469b7fe17..2a0efbff0 100644 --- a/env/server.ts +++ b/env/server.ts @@ -25,7 +25,6 @@ export const env = createEnv({ DESIGN_SYSTEM_ACCESS_TOKEN: z.string(), NEXTAUTH_REDIRECT_PROXY_URL: z.string().optional(), NEXTAUTH_SECRET: z.string(), - NEXTAUTH_URL: z.string(), NODE_ENV: z.enum(["development", "test", "production"]), PRINT_QUERY: z.boolean().default(false), REVALIDATE_SECRET: z.string(), @@ -55,7 +54,6 @@ export const env = createEnv({ DESIGN_SYSTEM_ACCESS_TOKEN: process.env.DESIGN_SYSTEM_ACCESS_TOKEN, NEXTAUTH_REDIRECT_PROXY_URL: process.env.NEXTAUTH_REDIRECT_PROXY_URL, NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET, - NEXTAUTH_URL: process.env.NEXTAUTH_URL, NODE_ENV: process.env.NODE_ENV, PRINT_QUERY: process.env.PRINT_QUERY, REVALIDATE_SECRET: process.env.REVALIDATE_SECRET, diff --git a/netlify.toml b/netlify.toml index 190b12b25..907045b47 100644 --- a/netlify.toml +++ b/netlify.toml @@ -2,14 +2,20 @@ command = "npm run build" publish = ".next" +[context.branch-deploy] +command = "npm run lint && npm run build" + +[context.deploy-preview] +command = "npm run lint && npm run build" + [[plugins]] package = "netlify-plugin-cypress" [plugins.inputs] configFile = "cypress.config.ts" [plugins.inputs.postBuild] -enable = true -start = "npm start" -wait-on = "http://127.0.0.1:3000/en/test" +enable = true +start = "npm start" +wait-on = "http://127.0.0.1:3000/en/test" wait-on-timeout = "30" # seconds [build.environment] diff --git a/next.config.js b/next.config.js index a2edb87b4..15832b42a 100644 --- a/next.config.js +++ b/next.config.js @@ -13,8 +13,21 @@ const jiti = createJiti(new URL(import.meta.url).pathname) jiti("./env/server") jiti("./env/client") +// We set NEXTAUTH_URL here because next.config.js is included in Netlify +// functions when bundling. Otherwise we are unable to login on preview +// deployments. We use DEPLOY_PRIME_URL from Netlify for all non-production +// branches. Locally we set it in .env.local instead of NEXTAUTH_URL. +// Netlify`s Next.js Runtime has built-in support for Next-Auth, +// but Next-Auth v5 is ESM and therefore not yet handle correctly by Netlify. +// This workaround should not be needed once Netlify fixes their code. /** @type {import('next').NextConfig} */ const nextConfig = { + env: { + NEXTAUTH_URL: + (process.env.CONTEXT === "production" + ? process.env.URL + : process.env.DEPLOY_PRIME_URL) + "/api/web/auth", + }, poweredByHeader: false, eslint: { ignoreDuringBuilds: true }, images: {