From 25828032a3dd7ac7dd724cb54fee77a6eb7b7870 Mon Sep 17 00:00:00 2001 From: Ubinquitous Date: Tue, 26 Sep 2023 19:45:47 +0900 Subject: [PATCH 1/7] =?UTF-8?q?chore(axiosError):=20if=EB=AC=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/error/throwAxiosError.ts | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/apis/error/throwAxiosError.ts b/src/apis/error/throwAxiosError.ts index 27a45432..e0163384 100644 --- a/src/apis/error/throwAxiosError.ts +++ b/src/apis/error/throwAxiosError.ts @@ -1,20 +1,11 @@ import { isAxiosError } from "axios"; -import { toast } from "react-toastify"; const throwAxiosError = (err: unknown) => { - if (!isAxiosError(err)) - return { - data: "", - status: "", - message: "", - }; + if (isAxiosError(err) && err.response) { + const { code, status, message } = err.response.data; - const data = err?.response?.data; - const { code, status, message } = data; - console.log(err); - toast.error("오류가 발생했습니다."); - - return { code, status, message }; + return { code, status, message }; + } }; export default throwAxiosError; From 13877979dbfd709186ba65d4ea1f3f6aab8ccf32 Mon Sep 17 00:00:00 2001 From: Ubinquitous Date: Tue, 26 Sep 2023 19:46:02 +0900 Subject: [PATCH 2/7] =?UTF-8?q?fix(httpClient):=20interceptor=20refresh=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/httpClient/httpClient.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/apis/httpClient/httpClient.ts b/src/apis/httpClient/httpClient.ts index 2fe5851d..2230d82c 100644 --- a/src/apis/httpClient/httpClient.ts +++ b/src/apis/httpClient/httpClient.ts @@ -1,7 +1,6 @@ import axios, { AxiosInstance, AxiosRequestConfig } from "axios"; import { requestInterceptors, responseInterceptors } from "@/apis/interceptor"; -import { KEY, TOKEN } from "@/constants/"; -import { QueryClient } from "@tanstack/react-query"; +import { TOKEN } from "@/constants/"; import Storage from "../storage"; export interface HttpClientConfig { @@ -22,9 +21,7 @@ export class HttpClient { withCredentials: true, }); HttpClient.clientConfig = { - headers: { - Authorization: Storage.getItem(TOKEN.ACCESS) || "", - }, + headers: { Authorization: Storage.getItem(TOKEN.ACCESS) || "" }, }; this.setting(); } @@ -125,15 +122,10 @@ export class HttpClient { private setting() { HttpClient.setCommonInterceptors(this.api); - const queryClient = new QueryClient(); this.api.interceptors.response.use( (response) => response, (error) => { - queryClient.invalidateQueries([ - KEY.USER, - Storage.getItem(TOKEN.ACCESS), - ]); return Promise.reject(error); }, ); From 5567483c8fcfc1edad12be8e247563b75b671713 Mon Sep 17 00:00:00 2001 From: Ubinquitous Date: Tue, 26 Sep 2023 19:46:11 +0900 Subject: [PATCH 3/7] =?UTF-8?q?chore(interceptor):=20getToken=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/interceptor/index.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/apis/interceptor/index.ts b/src/apis/interceptor/index.ts index acc229cd..2de78663 100644 --- a/src/apis/interceptor/index.ts +++ b/src/apis/interceptor/index.ts @@ -1,10 +1,9 @@ import { AxiosRequestConfig, AxiosResponse } from "axios"; -import { TOKEN } from "@/constants"; -import Storage from "../storage"; +import { getToken } from "@/helpers"; export const requestInterceptors = (requestConfig: AxiosRequestConfig) => { const urlParams = requestConfig.url?.split("/:") || []; - const accessToken = Storage.getItem(TOKEN.ACCESS); + const accessToken = getToken(); if (accessToken && requestConfig.headers) requestConfig.headers.Authorization = accessToken; From 0f1a689189b411e1288c38f053f4588c91c8461f Mon Sep 17 00:00:00 2001 From: Ubinquitous Date: Tue, 26 Sep 2023 19:46:19 +0900 Subject: [PATCH 4/7] =?UTF-8?q?chore(token):=20getToken=EC=9C=BC=EB=A1=9C?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/token/authorization.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/apis/token/authorization.ts b/src/apis/token/authorization.ts index 1463c2e4..c8738401 100644 --- a/src/apis/token/authorization.ts +++ b/src/apis/token/authorization.ts @@ -1,9 +1,8 @@ -import Storage from "@/apis/storage"; -import { TOKEN } from "@/constants"; +import { getToken } from "@/helpers"; const authorization = () => ({ headers: { - Authorization: `Bearer ${Storage.getItem(TOKEN.ACCESS)}`, + Authorization: `Bearer ${getToken()}`, }, }); From d44404ca0e82f40c256ff5b915bf8551ffdb90c6 Mon Sep 17 00:00:00 2001 From: Ubinquitous Date: Tue, 26 Sep 2023 19:54:47 +0900 Subject: [PATCH 5/7] =?UTF-8?q?fix(httpClient):=20access=20token=20expired?= =?UTF-8?q?=20=EB=A1=9C=EC=A7=81=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/httpClient/httpClient.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/apis/httpClient/httpClient.ts b/src/apis/httpClient/httpClient.ts index 2230d82c..78a7f167 100644 --- a/src/apis/httpClient/httpClient.ts +++ b/src/apis/httpClient/httpClient.ts @@ -1,7 +1,8 @@ import axios, { AxiosInstance, AxiosRequestConfig } from "axios"; import { requestInterceptors, responseInterceptors } from "@/apis/interceptor"; -import { TOKEN } from "@/constants/"; +import { ERROR, TOKEN } from "@/constants/"; import Storage from "../storage"; +import { refresh } from "../token"; export interface HttpClientConfig { baseURL?: string; @@ -126,6 +127,11 @@ export class HttpClient { this.api.interceptors.response.use( (response) => response, (error) => { + if (error.response) { + const { code } = error.response.data; + + if (code === ERROR.CODE.TOKEN_403_2) refresh(); + } return Promise.reject(error); }, ); From ca29d0f2f86d2f3c6eed407440782f2e8d2e9f45 Mon Sep 17 00:00:00 2001 From: Ubinquitous Date: Tue, 26 Sep 2023 19:55:05 +0900 Subject: [PATCH 6/7] =?UTF-8?q?fix(refresh):=20=EC=83=88=EB=A1=9C=EC=9A=B4?= =?UTF-8?q?=20instance=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/token/refresh.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/apis/token/refresh.ts b/src/apis/token/refresh.ts index f0f9971f..3d5a4738 100644 --- a/src/apis/token/refresh.ts +++ b/src/apis/token/refresh.ts @@ -1,16 +1,16 @@ import { TOKEN } from "@/constants/"; import Storage from "@/apis/storage"; -import httpClient from "../httpClient"; +import axios from "axios"; + +const instance = axios.create({ + baseURL: process.env.NEXT_PUBLIC_BASE_URL, +}); const refresh = async () => { - try { - const { data } = await httpClient.refresh.put({ - refreshToken: `${Storage.getItem(TOKEN.REFRESH)}`, - }); - Storage.setItem(TOKEN.ACCESS, data.accessToken); - } catch (err) { - Storage.clear(); - } + const { data } = await instance.put("/api/auth/refresh/access", { + refreshToken: `${Storage.getItem(TOKEN.REFRESH)}`, + }); + Storage.setItem(TOKEN.ACCESS, data.accessToken); }; export default refresh; From 86f3ea8264df9185ba9dab4a758c5a04b4e7859a Mon Sep 17 00:00:00 2001 From: Ubinquitous Date: Tue, 26 Sep 2023 19:55:23 +0900 Subject: [PATCH 7/7] =?UTF-8?q?chore(useUser):=20userInfo=20=EC=95=A1?= =?UTF-8?q?=EC=84=B8=EC=8A=A4=ED=86=A0=ED=81=B0=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useUser.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/hooks/useUser.ts b/src/hooks/useUser.ts index 2df16d3c..6b29784e 100644 --- a/src/hooks/useUser.ts +++ b/src/hooks/useUser.ts @@ -26,11 +26,11 @@ const useUser = (options?: UseUserOptions) => { const { data: userInfo, remove, - isLoading, error, refetch, + isLoading, } = useQuery( - [KEY.USER, Storage.getItem(TOKEN.ACCESS)], + [KEY.USER], async () => { const { data } = await httpClient.user.get(authorization()); return data; @@ -47,7 +47,6 @@ const useUser = (options?: UseUserOptions) => { React.useEffect(() => { if (isAxiosError(error) && error.response) { const { code } = error.response.data; - if (code === ERROR.CODE.TOKEN_403_2) refresh().then(() => refetch()); } }, [error, refetch]);