From e1ab37cdf5f64582bf135b497f5185d829d7e22c Mon Sep 17 00:00:00 2001 From: Ethen1264 Date: Tue, 27 Aug 2024 19:13:09 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9BFix:=20set=20header=20token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/Libs/apiClient.ts | 3 +++ src/utils/Libs/setHeader.ts | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/utils/Libs/setHeader.ts diff --git a/src/utils/Libs/apiClient.ts b/src/utils/Libs/apiClient.ts index e853eb90..2b44ffdc 100644 --- a/src/utils/Libs/apiClient.ts +++ b/src/utils/Libs/apiClient.ts @@ -2,12 +2,15 @@ import axios from 'axios'; import BASE_HEADER from '../Config/Config.json'; import { tokenReissue } from 'api/member'; import { getToken } from './getToken'; +import { setHeader } from './setHeader'; export const apiClient = axios.create({ baseURL: process.env.NEXT_PUBLIC_BASE_URL, headers: BASE_HEADER, }); +apiClient.interceptors.request.use(setHeader); + apiClient.interceptors.response.use( function (response) { return response; diff --git a/src/utils/Libs/setHeader.ts b/src/utils/Libs/setHeader.ts new file mode 100644 index 00000000..e2d06096 --- /dev/null +++ b/src/utils/Libs/setHeader.ts @@ -0,0 +1,18 @@ +import { AxiosRequestConfig } from 'axios'; +import { getToken } from './getToken'; +import { MemberController } from './requestUrls'; + +export const setHeader = async (config: AxiosRequestConfig) => { + if (typeof window !== 'object') return config; + const { Authorization } = await getToken(null); + + if ( + config.headers && + Authorization && + !config.url?.includes(MemberController.auth) + ) { + config.headers['Authorization'] = Authorization; + } + + return config; +}; From c635474f0eca94de37b61a8e607d517adde42296 Mon Sep 17 00:00:00 2001 From: Ethen1264 Date: Wed, 28 Aug 2024 10:19:56 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8Update:=20getRefresh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/Libs/apiClient.ts | 40 ++++---------------------------- src/utils/Libs/getRefresh.ts | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 36 deletions(-) create mode 100644 src/utils/Libs/getRefresh.ts diff --git a/src/utils/Libs/apiClient.ts b/src/utils/Libs/apiClient.ts index 2b44ffdc..af54dcab 100644 --- a/src/utils/Libs/apiClient.ts +++ b/src/utils/Libs/apiClient.ts @@ -1,8 +1,7 @@ import axios from 'axios'; import BASE_HEADER from '../Config/Config.json'; -import { tokenReissue } from 'api/member'; -import { getToken } from './getToken'; import { setHeader } from './setHeader'; +import { getRefresh } from './getRefresh'; export const apiClient = axios.create({ baseURL: process.env.NEXT_PUBLIC_BASE_URL, @@ -11,37 +10,6 @@ export const apiClient = axios.create({ apiClient.interceptors.request.use(setHeader); -apiClient.interceptors.response.use( - function (response) { - return response; - }, - async function (err) { - const originalRequest = err.config; - - if ( - err.response && - (err.response.status === 401 || err.response.status === 403) && - !originalRequest._retry - ) { - originalRequest._retry = true; - - try { - const { RefreshToken } = await getToken(null); - const tokenResponse = await tokenReissue( - 'Bearer ' + RefreshToken, - null - ); - - if (tokenResponse && tokenResponse.newAuthorization) { - originalRequest.headers['Authorization'] = - 'Bearer ' + tokenResponse.newAuthorization; - return apiClient(originalRequest); - } - } catch (refreshError) { - return Promise.reject(refreshError); - } - } - - return Promise.reject(err); - } -); +apiClient.interceptors.response.use(function (response) { + return response; +}, getRefresh); diff --git a/src/utils/Libs/getRefresh.ts b/src/utils/Libs/getRefresh.ts new file mode 100644 index 00000000..c197a436 --- /dev/null +++ b/src/utils/Libs/getRefresh.ts @@ -0,0 +1,45 @@ +import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'; +import { getToken } from './getToken'; +import { tokenReissue } from 'api/member'; +import { apiClient } from './apiClient'; + +interface TokenResponse { + newAuthorization: string; +} + +export async function getRefresh( + err: AxiosError +): Promise> { + const originalRequest = err.config as AxiosRequestConfig & { + _retry?: boolean; + }; + + if ( + err.response && + (err.response.status === 401 || err.response.status === 403) && + !originalRequest._retry + ) { + originalRequest._retry = true; + + try { + const { RefreshToken } = (await getToken(null)) as { + RefreshToken: string; + }; + const tokenResponse = (await tokenReissue( + 'Bearer ' + RefreshToken, + null + )) as TokenResponse; + + if (tokenResponse && tokenResponse.newAuthorization) { + originalRequest.headers = originalRequest.headers || {}; + originalRequest.headers['Authorization'] = + 'Bearer ' + tokenResponse.newAuthorization; + return apiClient(originalRequest); + } + } catch (refreshError) { + return Promise.reject(refreshError); + } + } + + return Promise.reject(err); +}