Skip to content

Commit fddeb76

Browse files
committed
fixed
1 parent a50fd38 commit fddeb76

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

apps/frontend/lib/utils/fetch.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ import { SafeUser } from '@mtes/types';
22
import { GetServerSidePropsContext } from 'next';
33

44
export 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

1214
export 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

apps/frontend/next.config.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,41 @@ const nextConfig = {
1010
nx: {
1111
// Set this to true if you would like to to use SVGR
1212
// See: https://github.com/gregberge/svgr
13-
svgr: false,
13+
svgr: false
1414
},
1515

1616
compiler: {
1717
// For other options, see https://nextjs.org/docs/architecture/nextjs-compiler#emotion
18-
emotion: true,
18+
emotion: true
1919
},
2020

2121
// Enable standalone output for better Docker deployment
2222
output: 'standalone',
23+
24+
// Proxy API requests to the backend so cookies work on the same origin
25+
async rewrites() {
26+
const backendUrl =
27+
process.env.INTERNAL_API_URL || process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3333';
28+
return [
29+
{
30+
source: '/auth/:path*',
31+
destination: `${backendUrl}/auth/:path*`
32+
},
33+
{
34+
source: '/api/:path*',
35+
destination: `${backendUrl}/api/:path*`
36+
},
37+
{
38+
source: '/public/:path*',
39+
destination: `${backendUrl}/public/:path*`
40+
}
41+
];
42+
}
2343
};
2444

2545
const plugins = [
2646
// Add more Next.js plugins to this list if needed.
27-
withNx,
47+
withNx
2848
];
2949

3050
module.exports = composePlugins(...plugins)(nextConfig);

0 commit comments

Comments
 (0)