feat: improve handling of deployment env vars

These are now defined in Netlify UI for dedicated environments (test, stage, production):

AUTH_URL
NEXTAUTH_URL
PUBLIC_URL

Code now falls back to incoming request host. Mainly used for
deployment previews which do not have Akamai in front, meaning
we do not need the above workaround as incoming request host
matches the actual public facing host. When Akamai is in front,
we lose the public facing host in Netlify's routing layer as they
internally use `x-forwarded-for` and we can't claim it for our usage.
This commit is contained in:
Michael Zetterberg
2024-10-14 20:43:53 +02:00
parent 3a3491c534
commit 4a846540c3
26 changed files with 72 additions and 485 deletions

View File

@@ -2,8 +2,7 @@ import { NextResponse } from "next/server"
import { authRequired, mfaRequired } from "@/constants/routes/authRequired"
import { login } from "@/constants/routes/handleAuth"
import { env } from "@/env/server"
import { internalServerError } from "@/server/errors/next"
import { getPublicNextURL } from "@/server/utils"
import { auth } from "@/auth"
import { findLang } from "@/utils/languages"
@@ -44,14 +43,7 @@ export const middleware = auth(async (request) => {
const isLoggedIn = !!request.auth
const hasError = request.auth?.error
if (!env.PUBLIC_URL) {
throw internalServerError("Missing value for env.PUBLIC_URL")
}
const publicUrl = new URL(env.PUBLIC_URL)
const nextUrlPublic = nextUrl.clone()
nextUrlPublic.host = publicUrl.host
nextUrlPublic.hostname = publicUrl.hostname
const nextUrlPublic = getPublicNextURL(request)
/**
* Function to validate MFA from token data

View File

@@ -2,6 +2,7 @@ import { NextResponse } from "next/server"
import { env } from "@/env/server"
import { badRequest, internalServerError } from "@/server/errors/next"
import { getPublicURL } from "@/server/utils"
import { findLang } from "@/utils/languages"
@@ -16,10 +17,7 @@ export const middleware: NextMiddleware = (request) => {
}
const lang = findLang(request.nextUrl.pathname)!
if (!env.PUBLIC_URL) {
throw internalServerError("No value for env.PUBLIC_URL")
}
const redirectTo = env.PUBLIC_URL
const redirectTo = getPublicURL(request)
const headers = new Headers(request.headers)
headers.set("x-returnurl", redirectTo)

View File

@@ -8,6 +8,7 @@ import {
} from "@/constants/routes/myPages"
import { env } from "@/env/server"
import { internalServerError, notFound } from "@/server/errors/next"
import { getPublicNextURL } from "@/server/utils"
import { findLang } from "@/utils/languages"
@@ -23,17 +24,9 @@ export const middleware: NextMiddleware = async (request) => {
const myPagesRoot = myPages[lang]
if (nextUrl.pathname === myPagesRoot) {
if (!env.PUBLIC_URL) {
throw internalServerError("Missing value for env.PUBLIC_URL")
}
const publicUrl = new URL(env.PUBLIC_URL)
const nextUrlClone = nextUrl.clone()
nextUrlClone.host = publicUrl.host
nextUrlClone.hostname = publicUrl.hostname
const nextUrlPublic = getPublicNextURL(request)
const overviewUrl = overview[lang]
const redirectUrl = new URL(overviewUrl, nextUrlClone)
const redirectUrl = new URL(overviewUrl, nextUrlPublic)
console.log(`[myPages] redirecting to: ${redirectUrl}`)
return NextResponse.redirect(redirectUrl)
}

View File

@@ -1,7 +1,5 @@
import { stringify } from "querystring"
import { Lang } from "@/constants/languages"
import { env } from "@/env/server"
import { getPublicNextURL } from "@/server/utils"
import { resolve as resolveEntry } from "@/utils/entry"
import { findLang } from "@/utils/languages"
@@ -11,18 +9,7 @@ import type { NextRequest } from "next/server"
export function getDefaultRequestHeaders(request: NextRequest) {
const lang = findLang(request.nextUrl.pathname)!
let nextUrl
if (env.PUBLIC_URL) {
const publicUrl = new URL(env.PUBLIC_URL)
const nextUrlPublic = request.nextUrl.clone()
nextUrlPublic.host = publicUrl.host
nextUrlPublic.hostname = publicUrl.hostname
nextUrl = nextUrlPublic
} else {
nextUrl = request.nextUrl
}
const nextUrlPublic = getPublicNextURL(request)
const headers = new Headers(request.headers)
headers.set("x-lang", lang)
headers.set(
@@ -31,7 +18,7 @@ export function getDefaultRequestHeaders(request: NextRequest) {
request.nextUrl.pathname.replace(`/${lang}`, "").replace(`/webview`, "")
)
)
headers.set("x-url", removeTrailingSlash(nextUrl.href))
headers.set("x-url", removeTrailingSlash(nextUrlPublic.href))
return headers
}