From 80c88ca43174e950e27e3645e0a93e3ced7c44fa Mon Sep 17 00:00:00 2001 From: juno Date: Fri, 13 Feb 2026 23:44:03 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=ED=81=AC=EB=A1=9C=EC=8A=A4=20=EC=98=A4?= =?UTF-8?q?=EB=A6=AC=EC=A7=84=20API=20=EC=9D=B8=EC=A6=9D=20=EC=8B=9C=20Bea?= =?UTF-8?q?rer=20=ED=86=A0=ED=81=B0=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Web/API 도메인이 달라 쿠키 미전송 → getSession()으로 토큰 조회 후 Authorization 헤더에 설정 - api.ts 요청 인터셉터에 Bearer 토큰 주입 로직 추가 --- web/src/lib/__tests__/api.test.ts | 23 +++++++++-------------- web/src/lib/api.ts | 15 ++++++++++++--- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/web/src/lib/__tests__/api.test.ts b/web/src/lib/__tests__/api.test.ts index 97183a0..7c47cde 100644 --- a/web/src/lib/__tests__/api.test.ts +++ b/web/src/lib/__tests__/api.test.ts @@ -1,8 +1,14 @@ import api from '../api'; +jest.mock('@/lib/auth-client', () => ({ + authClient: { + getSession: jest.fn().mockResolvedValue({ data: null }), + }, +})); + /** * API Client 단위 테스트 - * 쿠키 기반 인증으로 전환 후 Bearer Token 추출 로직이 제거되었는지 확인합니다. + * 프로덕션 크로스 오리진 환경에서 Bearer 토큰으로 인증합니다. */ describe('API Client', () => { describe('Axios 설정', () => { @@ -20,20 +26,9 @@ describe('API Client', () => { }); describe('요청 인터셉터', () => { - it('should not have request interceptors that set Authorization header', () => { - // Bearer Token 추출 로직이 제거되었으므로 - // 요청 인터셉터가 없거나 Authorization 헤더를 설정하지 않아야 함 + it('should have request interceptor for Bearer token', () => { const requestInterceptors = api.interceptors.request.handlers; - - // 요청 인터셉터가 없거나, Authorization 헤더를 설정하지 않는지 확인 - if (requestInterceptors.length > 0) { - // 인터셉터가 있다면 Authorization 헤더를 설정하지 않는지 확인 - // 실제로는 인터셉터가 없어야 함 (Bearer Token 로직 제거됨) - expect(requestInterceptors.length).toBe(0); - } else { - // 인터셉터가 없는 것이 정상 (Bearer Token 로직 제거됨) - expect(requestInterceptors.length).toBe(0); - } + expect(requestInterceptors.length).toBeGreaterThan(0); }); }); diff --git a/web/src/lib/api.ts b/web/src/lib/api.ts index e3206fb..78facb7 100644 --- a/web/src/lib/api.ts +++ b/web/src/lib/api.ts @@ -1,6 +1,7 @@ import axios, { AxiosError } from "axios"; import type { ApiError, ApiErrorResponse } from "@/types/api"; import { API_URL } from "@/constants/api"; +import { authClient } from "@/lib/auth-client"; // 하위 호환성을 위해 타입 re-export export type { ApiError, ApiErrorResponse } from "@/types/api"; @@ -10,11 +11,19 @@ const api = axios.create({ headers: { "Content-Type": "application/json", }, - withCredentials: true, // 쿠키 자동 전송 (Better-Auth 세션 쿠키 포함) + withCredentials: true, }); -// Bearer Token 추출 로직 제거 -// 쿠키가 자동으로 전송되므로 추가 작업 불필요 +// 프로덕션: Web/API 도메인이 달라 쿠키 미전송 → Bearer 토큰으로 인증 +api.interceptors.request.use(async (config) => { + if (typeof window === "undefined") return config; + + const { data: session } = await authClient.getSession(); + if (session?.session?.token) { + config.headers.Authorization = `Bearer ${session.session.token}`; + } + return config; +}); api.interceptors.response.use( (response) => response,