From 9996a77ec0f7bf64f8c912ee9d3185ef5226af55 Mon Sep 17 00:00:00 2001 From: hyuna Date: Sun, 14 Jul 2024 18:39:14 +0900 Subject: [PATCH] =?UTF-8?q?chore=20:=20=EB=A6=AC=ED=94=84=EB=A0=88?= =?UTF-8?q?=EC=89=AC=ED=86=A0=ED=81=B0=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/index.ts | 39 ++++++++++++++------------------------- src/app/login/page.tsx | 2 +- src/hook/apiError.ts | 4 +--- 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/apis/index.ts b/src/apis/index.ts index 48e1aa5..a04065c 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -1,6 +1,5 @@ import axios, { AxiosError } from "axios"; import { cookie } from "@/utils/auth"; -import { useRouter } from "next/navigation"; const BASEURL = process.env.NEXT_PUBLIC_API_KEY; @@ -38,40 +37,30 @@ refreshInstance.interceptors.request.use( instance.interceptors.response.use( (response) => response, - async (error: AxiosError) => { + async (error: AxiosError) => { if (axios.isAxiosError(error) && error.response) { - const { status } = error.response.data; + const { status } = error.response; if (status === 401) { const refreshToken = cookie.get("refresh_token"); if (refreshToken) { try { - const res = await axios.put( - `${BASEURL}/refresh`, - {}, - { + await axios + .put(`${BASEURL}/admin/refresh`, null, { headers: { - "X-Refresh-Token": `Bearer ${refreshToken}`, + "X-Refresh-Token": `${refreshToken}`, }, - } - ); - const { data } = res.data; - const accessToken = data.accessToken; - cookie.set("access_token", accessToken); - if (error.config) { - error.config.headers.Authorization = `Bearer ${accessToken}`; - return axios.request(error.config); - } - } catch { - throw error; + }) + .then((response) => { + const data = response.data; + cookie.set("access_token", data.access_token); + cookie.set("refresh_token", data.refresh_token); + }); + } catch (refreshError) { + return Promise.reject(refreshError); } - } else { - throw error; } - } else { - throw error; } - } else { - throw error; } + return Promise.reject(error); } ); diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index ddc9350..a6a225f 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -39,7 +39,7 @@ const Login: NextPage = () => { const onClickBtn = async () => { try { - const result = await loginMutate(data, { + await loginMutate(data, { onSuccess: (res) => { const accessToken = res.access_token; const refreshToken = res.refresh_token; diff --git a/src/hook/apiError.ts b/src/hook/apiError.ts index f85d191..5372839 100644 --- a/src/hook/apiError.ts +++ b/src/hook/apiError.ts @@ -14,9 +14,7 @@ const ApiError = () => { alert("400 잘못된 요청입니다"); }; - const handle401: ErrorHandler = () => { - router.push("login"); - }; + const handle401: ErrorHandler = () => {}; const handle403: ErrorHandler = () => { alert("403 권한이 없습니다");