@@ -2,19 +2,21 @@ import { SafeUser } from '@mtes/types';
22import { GetServerSidePropsContext } from 'next' ;
33
44export const getApiBase = ( isServerSide : boolean = false ) : string => {
5- const publicApiUrl = process . env . NEXT_PUBLIC_API_URL ?? 'http://localhost:3333' ;
5+ // For client-side requests, use empty string to go through Next.js rewrites (same origin)
6+ // For server-side requests (SSR), use the internal API URL directly
7+ if ( ! isServerSide ) {
8+ return '' ; // Client-side: use relative URLs, Next.js rewrites handle the proxy
9+ }
610 const internalApiUrl = process . env . INTERNAL_API_URL ?? 'http://backend:3333' ;
7-
8- // Use internal API URL for server-side rendering, public API URL for client-side
9- return isServerSide ? internalApiUrl : publicApiUrl ;
11+ return internalApiUrl ;
1012} ;
1113
1214export const apiFetch = (
1315 path : string ,
1416 init ?: RequestInit | undefined ,
1517 ctx ?: GetServerSidePropsContext
1618) : Promise < Response > => {
17- let headers = { ...init ?. headers } ;
19+ let headers : Record < string , string > = { ...( init ?. headers as Record < string , string > ) } ;
1820 if ( ctx ) {
1921 let token : string | undefined = undefined ;
2022 const authHeader = ctx . req . headers . authorization as string ;
@@ -25,7 +27,12 @@ export const apiFetch = (
2527 }
2628 // Only add Authorization header if token exists
2729 if ( token ) {
28- headers = { Authorization : `Bearer ${ token } ` , ...init ?. headers } ;
30+ headers [ 'Authorization' ] = `Bearer ${ token } ` ;
31+ }
32+ // Forward cookies from the incoming request for SSR
33+ const cookieHeader = ctx . req . headers . cookie ;
34+ if ( cookieHeader ) {
35+ headers [ 'Cookie' ] = cookieHeader ;
2936 }
3037 }
3138 // Use server-side URL when we have server context (SSR), client-side URL otherwise
0 commit comments