diff --git a/apps/chat/apps/web/src/hooks.server.ts b/apps/chat/apps/web/src/hooks.server.ts index 392ae8c7e..6d7a5089d 100644 --- a/apps/chat/apps/web/src/hooks.server.ts +++ b/apps/chat/apps/web/src/hooks.server.ts @@ -1,10 +1,27 @@ /** * Server Hooks for SvelteKit - * Auth is now handled client-side via Mana Core Auth + * - Injects runtime environment variables for client-side use + * - Auth is handled client-side via Mana Core Auth */ import type { Handle } from '@sveltejs/kit'; +// Get client-side URLs from environment (Docker runtime) +const PUBLIC_MANA_CORE_AUTH_URL_CLIENT = + process.env.PUBLIC_MANA_CORE_AUTH_URL_CLIENT || process.env.PUBLIC_MANA_CORE_AUTH_URL || ''; +const PUBLIC_BACKEND_URL_CLIENT = + process.env.PUBLIC_BACKEND_URL_CLIENT || process.env.PUBLIC_BACKEND_URL || ''; + export const handle: Handle = async ({ event, resolve }) => { - return resolve(event); + return resolve(event, { + transformPageChunk: ({ html }) => { + // Inject runtime environment variables into the HTML + // These will be available on window.__PUBLIC_*__ for client-side code + const envScript = ``; + return html.replace('', `${envScript}`); + }, + }); }; diff --git a/apps/chat/apps/web/src/lib/stores/auth.svelte.ts b/apps/chat/apps/web/src/lib/stores/auth.svelte.ts index 93a166a78..6e9647cf3 100644 --- a/apps/chat/apps/web/src/lib/stores/auth.svelte.ts +++ b/apps/chat/apps/web/src/lib/stores/auth.svelte.ts @@ -10,15 +10,13 @@ import type { UserData } from '@manacore/shared-auth'; // Get auth URL dynamically at runtime - fallback for SSR and client function getAuthUrl(): string { if (browser && typeof window !== 'undefined') { - // Client-side: check for injected env or use default - return ( - (window as unknown as { __PUBLIC_MANA_CORE_AUTH_URL__?: string }) - .__PUBLIC_MANA_CORE_AUTH_URL__ || - import.meta.env.PUBLIC_MANA_CORE_AUTH_URL || - 'http://localhost:3001' - ); + // Client-side: use injected window variable (set by hooks.server.ts) + // Falls back to localhost for local development + const injectedUrl = (window as unknown as { __PUBLIC_MANA_CORE_AUTH_URL__?: string }) + .__PUBLIC_MANA_CORE_AUTH_URL__; + return injectedUrl || 'http://localhost:3001'; } - // Server-side: use process.env or default + // Server-side (SSR): use Docker internal URL for container-to-container communication return process.env.PUBLIC_MANA_CORE_AUTH_URL || 'http://localhost:3001'; } diff --git a/docker-compose.staging.yml b/docker-compose.staging.yml index 771c7ced5..ce43ac37e 100644 --- a/docker-compose.staging.yml +++ b/docker-compose.staging.yml @@ -135,8 +135,12 @@ services: environment: NODE_ENV: staging PORT: 3000 + # Server-side URLs (Docker internal network) PUBLIC_BACKEND_URL: http://chat-backend:3002 PUBLIC_MANA_CORE_AUTH_URL: http://mana-core-auth:3001 + # Client-side URLs (browser access via public IP) + PUBLIC_BACKEND_URL_CLIENT: http://46.224.108.214:3002 + PUBLIC_MANA_CORE_AUTH_URL_CLIENT: http://46.224.108.214:3001 ports: - "3000:3000" healthcheck: