Commit 8138e182 authored by Anthony Jacob's avatar Anthony Jacob
Browse files

still trying to fix prod url management

parent cf9f646c
Loading
Loading
Loading
Loading
+21 −1
Original line number Diff line number Diff line
import type { NextConfig } from "next";

const configuredAppUrl = process.env.APP_URL || process.env.NEXT_PUBLIC_APP_URL || ''

let configuredHost = ''
if (configuredAppUrl) {
  try {
    configuredHost = new URL(configuredAppUrl).host
  } catch {
    configuredHost = ''
  }
}

const allowedServerActionOrigins = [
  'tradingbot.anthony-jacob.com',
  configuredHost,
].filter(Boolean)

const nextConfig: NextConfig = {
  /*turbopack: {
    root: "C:\\Users\\antho\\dev_workspace\\tradingbot-front",
  },*/
  /* config options here */
  experimental: {
    serverActions: {
      allowedOrigins: allowedServerActionOrigins,
    },
  },
};

export default nextConfig;
+58 −2
Original line number Diff line number Diff line
@@ -5,6 +5,61 @@ import { buildExternalUrl } from './libs/url'

const PUBLIC_PATHS = ['/login', '/api', '/_next/', '/favicon.ico', '/public/', '/_next/static']

function getForwardedValue(value: string | null): string | null {
  return value?.split(',')[0]?.trim() || null
}

function isInvalidHost(value: string | null): boolean {
  if (!value) return true
  const normalized = value.trim().toLowerCase()
  return normalized === 'null' || normalized === '(null)' || normalized === 'undefined'
}

function getOriginHost(origin: string | null): string | null {
  if (!origin) return null
  try {
    return new URL(origin).host || null
  } catch {
    return null
  }
}

function getOriginProto(origin: string | null): string | null {
  if (!origin) return null
  try {
    return new URL(origin).protocol.replace(':', '') || null
  } catch {
    return null
  }
}

function normalizeForwardedHeaders(req: NextRequest): Headers {
  const headers = new Headers(req.headers)

  const forwardedHostRaw = getForwardedValue(req.headers.get('x-forwarded-host'))
  const hostRaw = getForwardedValue(req.headers.get('host'))
  const originHost = getOriginHost(req.headers.get('origin'))

  const forwardedHost = isInvalidHost(forwardedHostRaw) ? null : forwardedHostRaw
  const host = isInvalidHost(hostRaw) ? null : hostRaw
  const resolvedHost = forwardedHost ?? originHost ?? host

  if (resolvedHost) {
    headers.set('x-forwarded-host', resolvedHost)
  }

  const forwardedProto = getForwardedValue(req.headers.get('x-forwarded-proto'))
  const originProto = getOriginProto(req.headers.get('origin'))
  const nextUrlProto = req.nextUrl.protocol.replace(':', '')
  const resolvedProto = forwardedProto ?? originProto ?? nextUrlProto

  if (resolvedProto) {
    headers.set('x-forwarded-proto', resolvedProto)
  }

  return headers
}

function isPublic(pathname: string) {
  return PUBLIC_PATHS.some(p => pathname.startsWith(p))
}
@@ -14,7 +69,8 @@ export async function proxy(req: NextRequest) {
  console.log(`${new Date().toLocaleString('fr-FR', { dateStyle: 'short', timeStyle: 'medium' })}:`, 'Middleware invoked for:', req.nextUrl.pathname);

  const { pathname } = req.nextUrl
  if (isPublic(pathname)) return NextResponse.next()
  const normalizedHeaders = normalizeForwardedHeaders(req)
  if (isPublic(pathname)) return NextResponse.next({ request: { headers: normalizedHeaders } })

  const access_token = req.cookies.get('access_token')?.value
  const refreshToken = req.cookies.get('refresh_token')?.value
@@ -56,7 +112,7 @@ export async function proxy(req: NextRequest) {
      }

      console.log('JWT verified successfully');
      return NextResponse.next()
      return NextResponse.next({ request: { headers: normalizedHeaders } })

    } catch (err) {