From 8f45e5b0b89976dd5878bbfa9871e9344bcb852b Mon Sep 17 00:00:00 2001 From: seoyeong Date: Tue, 20 Feb 2024 12:54:02 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20axios=20interceptor=EC=97=90=EC=84=9C?= =?UTF-8?q?=20=ED=86=A0=ED=81=B0=EC=9D=B4=20=EC=BF=A0=ED=82=A4=EC=97=90=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=EB=90=98=EC=96=B4=20=EC=9E=88=EB=8B=A4?= =?UTF-8?q?=EB=A9=B4=20=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95=20#179?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/remote/api/common.api.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/remote/api/common.api.ts b/src/remote/api/common.api.ts index 3720c5a3..ad2e50d2 100644 --- a/src/remote/api/common.api.ts +++ b/src/remote/api/common.api.ts @@ -1,4 +1,6 @@ /* eslint-disable no-param-reassign */ +import { useCookies } from 'react-cookie'; + import axios, { InternalAxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'; import { axiosInstance } from '@remote/api/instance.api'; @@ -10,13 +12,14 @@ axiosInstance.interceptors.request.use( * request 직전 공통으로 진행할 작업 */ if (config && config.headers) { - // TODO: 인증할 때 받은 토큰을 쿠키에 저장했다면 가져옵니다. + // 인증할 때 받은 토큰을 쿠키에 저장했다면 가져옵니다. + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const [cookies, setCookie] = useCookies(['token']); - // const token = getCookie('token'); - // if (token) { - // config.headers.Authorization = `Bearer ${token}`; - // config.headers['Content-Type'] = 'application/json'; - // } + if (cookies.token) { + config.headers.Authorization = `Bearer ${cookies.token}`; + config.headers['Content-Type'] = 'application/json'; + } } if (process.env.NODE_ENV === 'development') { const { method, url } = config;