From 1bfc5df767b6d284283186a0d9628bb7a38c0195 Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Mon, 10 Mar 2025 17:56:24 +0000 Subject: [PATCH] ...... --- lib/auth-helpers.ts | 27 +++++++++++++++++++++++---- lib/server-service.ts | 24 +++++++++++++++++++++--- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/auth-helpers.ts b/lib/auth-helpers.ts index bcb7b7f..51c36f8 100644 --- a/lib/auth-helpers.ts +++ b/lib/auth-helpers.ts @@ -3,11 +3,30 @@ // Helper function to verify authentication with the local API export async function verifyAuth(token: string) { try { - // Use a properly formed URL with origin - const origin = typeof window !== 'undefined' ? window.location.origin : ''; - const authEndpoint = new URL('/api/auth/me', origin).toString(); + let authEndpoint; - console.log(`Verifying auth with ${authEndpoint} using token: ${token.substring(0, 10)}...`); + // Check if we're running in a browser environment + if (typeof window !== 'undefined') { + // Use the current origin in browser environments + const origin = window.location.origin; + authEndpoint = new URL('/api/auth/me', origin).toString(); + console.log(`Using browser origin for auth endpoint: ${authEndpoint}`); + } else { + // For SSR or when window is not available (e.g. in Docker container) + // Use the environment variable if available + if (process.env.SERVER_API_URL) { + authEndpoint = `${process.env.SERVER_API_URL}/auth/me`; + console.log(`Using SERVER_API_URL for auth endpoint: ${authEndpoint}`); + } else { + // Fallback for local development + const protocol = (process.env.USE_HTTPS === 'false') ? 'http' : 'https'; + const port = process.env.INTERNAL_API_PORT || '3000'; + authEndpoint = `${protocol}://localhost:${port}/api/auth/me`; + console.log(`Using fallback for auth endpoint: ${authEndpoint}`); + } + } + + console.log(`Verifying authentication with endpoint: ${authEndpoint}`); const response = await fetch(authEndpoint, { method: "GET", diff --git a/lib/server-service.ts b/lib/server-service.ts index 51649c1..d3badfb 100644 --- a/lib/server-service.ts +++ b/lib/server-service.ts @@ -5,12 +5,21 @@ import { redirect } from 'next/navigation'; * Gets the base URL for server API requests with proper fallbacks */ function getBaseUrl() { + // First check for the specific server API URL environment variable + if (process.env.SERVER_API_URL) { + console.log(`Using SERVER_API_URL: ${process.env.SERVER_API_URL}`); + return process.env.SERVER_API_URL; + } + // For server components, we normally would use environment variables // But we need to be careful with how they're accessed // Try to get the API URL from environment variables const apiUrl = process.env.NEXT_PUBLIC_API_URL; + // Check if we're running in a container/production environment + const inContainer = process.env.NODE_ENV === 'production'; + // We need to get the host from somewhere to construct the URL // In production, we can rely on the VERCEL_URL or similar if (process.env.VERCEL_URL) { @@ -23,12 +32,21 @@ function getBaseUrl() { if (apiUrl.startsWith('http')) { return apiUrl; } + + // For container environments, use the internal port + const port = process.env.INTERNAL_API_PORT || '3000'; + const protocol = (process.env.USE_HTTPS === 'false') ? 'http' : 'https'; + // Otherwise, it's likely a relative path like /api - return `http://localhost:3000${apiUrl.startsWith('/') ? apiUrl : `/${apiUrl}`}`; + // Use localhost with the correct port for container environments + return `${protocol}://localhost:${port}${apiUrl.startsWith('/') ? apiUrl : `/${apiUrl}`}`; } - // Last resort fallback for development - return 'http://localhost:3000/api'; + // Last resort fallback + // Use http for container environment, https for external services + const protocol = (process.env.USE_HTTPS === 'false') ? 'http' : 'https'; + const port = process.env.INTERNAL_API_PORT || '3000'; + return `${protocol}://localhost:${port}/api`; } /**