diff --git a/api.ts b/api.ts index d77eb87..afd202d 100644 --- a/api.ts +++ b/api.ts @@ -1,6 +1,8 @@ export const API_URL_ROOT = process.env.NEXT_PUBLIC_API_URL_ROOT || ""; export const URL_MEDIA = process.env.NEXT_PUBLIC_URL_MEDIA || ""; -export const HOME_URL = process.env.NEXT_PUBLIC_HOME_URL || "http://localhost:3000"; +export const HOME_URL = + process.env.NEXT_PUBLIC_HOME_URL || + (typeof window !== "undefined" ? window.location.origin : ""); export const API = { User : { CURRENT: `${API_URL_ROOT}/users/current`, @@ -70,4 +72,4 @@ export const API = { GET_ALL: `${API_URL_ROOT}/statistics`, GET_BY_DATE: (date: string) => `${API_URL_ROOT}/statistics/${date}`, } -} \ No newline at end of file +} diff --git a/next.config.ts b/next.config.ts index 532af9f..10ba076 100644 --- a/next.config.ts +++ b/next.config.ts @@ -19,6 +19,34 @@ const nextConfig: NextConfig = { ], }, output: 'standalone', + async rewrites() { + // Proxy backend API calls through Next.js to avoid browser CORS issues. + // + // Configure the target via: + // - API_PROXY_TARGET (server-side, recommended) e.g. http://localhost:8080 + // - NEXT_PUBLIC_API_URL_ROOT (fallback) + const target = + process.env.API_PROXY_TARGET || + process.env.NEXT_PUBLIC_API_URL_ROOT || + "https://history-api.kain.id.vn"; + + const prefixes = [ + "auth", + "users", + "media", + "projects", + "submissions", + "statistics", + "roles", + "historian", + ]; + return [ + ...prefixes.map((p) => ({ + source: `/${p}/:path*`, + destination: `${target}/${p}/:path*`, + })), + ]; + }, webpack(config) { config.module.rules.push({ test: /\.svg$/, @@ -37,4 +65,4 @@ const nextConfig: NextConfig = { }, }; -export default nextConfig; \ No newline at end of file +export default nextConfig; diff --git a/package.json b/package.json index 1903a1c..95b8e0a 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "private": true, "scripts": { "dev": "next dev", + "dev:3008": "next dev -p 3008", "build": "next build", "start": "next start", "lint": "eslint ." diff --git a/src/config/config.ts b/src/config/config.ts index 3f20e29..60905c3 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -1,7 +1,7 @@ import axios, { AxiosError, InternalAxiosRequestConfig } from "axios"; -export const baseURL = - process.env.NEXT_PUBLIC_API_URL_ROOT || process.env.NEXT_PUBLIC_API_URL || "http://localhost:3344"; +// Prefer same-origin requests. Next.js will proxy API paths via `next.config.ts` rewrites. +export const baseURL = "/"; export const api = axios.create({ baseURL, @@ -68,7 +68,7 @@ api.interceptors.response.use( isRefreshing = true; try { - await axios.post(`${baseURL}/auth/refresh`, undefined, { withCredentials: true }); + await axios.post("/auth/refresh", {}, { withCredentials: true }); processQueue(null);