From baa011fa08681379f85c04b4844e3c04d59d6668 Mon Sep 17 00:00:00 2001 From: Ubinquitous Date: Wed, 4 Oct 2023 05:19:17 +0900 Subject: [PATCH 1/4] =?UTF-8?q?delete(throwAxiosError):=20toast=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/error/throwAxiosError.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/apis/error/throwAxiosError.ts b/src/apis/error/throwAxiosError.ts index 27a45432..efd8dbbe 100644 --- a/src/apis/error/throwAxiosError.ts +++ b/src/apis/error/throwAxiosError.ts @@ -1,5 +1,4 @@ import { isAxiosError } from "axios"; -import { toast } from "react-toastify"; const throwAxiosError = (err: unknown) => { if (!isAxiosError(err)) @@ -12,7 +11,6 @@ const throwAxiosError = (err: unknown) => { const data = err?.response?.data; const { code, status, message } = data; console.log(err); - toast.error("오류가 발생했습니다."); return { code, status, message }; }; From bb188679286fc7418498e3bd1ced849c96595203 Mon Sep 17 00:00:00 2001 From: Ubinquitous Date: Wed, 4 Oct 2023 05:19:32 +0900 Subject: [PATCH 2/4] =?UTF-8?q?chore(httpClient):=20refresh=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/httpClient/httpClient.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/apis/httpClient/httpClient.ts b/src/apis/httpClient/httpClient.ts index 827e7c57..470d401e 100644 --- a/src/apis/httpClient/httpClient.ts +++ b/src/apis/httpClient/httpClient.ts @@ -1,8 +1,8 @@ import axios, { AxiosInstance, AxiosRequestConfig } from "axios"; import { requestInterceptors, responseInterceptors } from "@/apis/interceptor"; -import { KEY, TOKEN } from "@/constants/"; -import { QueryClient } from "@tanstack/react-query"; +import { ERROR, TOKEN } from "@/constants/"; import Storage from "../storage"; +import { refresh } from "../token"; export interface HttpClientConfig { baseURL?: string; @@ -132,16 +132,15 @@ 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); + async (error) => { + const originalRequest = error.config; + if (error.response.data.code === ERROR.CODE.TOKEN_403_2) { + await refresh(); + return this.api(originalRequest); + } }, ); } @@ -164,7 +163,6 @@ export default { post: new HttpClient("api/post/", axiosConfig), recomment: new HttpClient("api/recomment", axiosConfig), comment: new HttpClient("api/comment", axiosConfig), - refresh: new HttpClient("api/auth/refresh/access", axiosConfig), auth: new HttpClient("api/auth/", axiosConfig), bamboo: new HttpClient("api/bamboo", axiosConfig), admin: new HttpClient("api/bamboo/admin", axiosConfig), From 32ec762b74f70fe9c62764ccc591fc94d9f650de Mon Sep 17 00:00:00 2001 From: Ubinquitous Date: Wed, 4 Oct 2023 05:19:51 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix(refresh):=20new=20instance=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=ED=95=B4=20=EB=AC=B4=ED=95=9C=EC=9A=94?= =?UTF-8?q?=EC=B2=AD=20=EB=B0=A9=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/token/refresh.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/apis/token/refresh.ts b/src/apis/token/refresh.ts index f0f9971f..e0ca70c0 100644 --- a/src/apis/token/refresh.ts +++ b/src/apis/token/refresh.ts @@ -1,10 +1,14 @@ 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({ + const { data } = await instance.put("/api/auth/refresh/access", { refreshToken: `${Storage.getItem(TOKEN.REFRESH)}`, }); Storage.setItem(TOKEN.ACCESS, data.accessToken); From 552511bc8594d5a8efccc62603533ee56276a758 Mon Sep 17 00:00:00 2001 From: Ubinquitous Date: Wed, 4 Oct 2023 05:20:02 +0900 Subject: [PATCH 4/4] =?UTF-8?q?chore(user):=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=EC=95=A1=EC=84=B8=EC=8A=A4=ED=86=A0=ED=81=B0=20=EC=9D=98?= =?UTF-8?q?=EC=A1=B4=EC=84=B1=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useUser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/useUser.ts b/src/hooks/useUser.ts index 2df16d3c..b4851258 100644 --- a/src/hooks/useUser.ts +++ b/src/hooks/useUser.ts @@ -30,7 +30,7 @@ const useUser = (options?: UseUserOptions) => { error, refetch, } = useQuery( - [KEY.USER, Storage.getItem(TOKEN.ACCESS)], + [KEY.USER], async () => { const { data } = await httpClient.user.get(authorization()); return data;