Skip to content

Commit

Permalink
[#231] Fix: 서버 리프레시 토큰 관련 에러 핸들링 및 로그아웃 api에 누락된 header 추가 (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
choi-ik authored Mar 22, 2024
1 parent 05684b0 commit 7606f80
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/apis/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import { getLocalStorage } from "@/utils/localStorage";
import { GetUserInformationResponse } from "@/types/user.dto";
import { ReissueTokenResponse } from "@/types/auth.dto";
import http from "./core";
import { REFRESH_TOKEN } from "@/constants/auth";
import { ACCESS_TOKEN, REFRESH_TOKEN } from "@/constants/auth";

export const patchLogOut = async () => {
const refreshToken = getLocalStorage(REFRESH_TOKEN);

const accessToken = getLocalStorage(ACCESS_TOKEN);
try {
await axios.patch<void>(
`${import.meta.env.VITE_BASE_URL}/v1/user/logout`,
{},
{
headers: {
"Authorization-refresh": `Bearer ${refreshToken}`,
authorization: `Bearer ${accessToken}`,
"authorization-refresh": `Bearer ${refreshToken}`,
"Content-Type": "application/json",
},
},
Expand Down
13 changes: 12 additions & 1 deletion src/apis/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,20 @@ axiosInstance.interceptors.response.use(

return response.data;
},
(error) => {
async (error) => {
Sentry.captureException(error);

if (!axios.isAxiosError(error)) return;
if (
error.response?.status === 401 &&
error.response?.data.message === "refresh token이 유효하지 않습니다."
) {
removeLocalStorage(ACCESS_TOKEN);
removeLocalStorage(REFRESH_TOKEN);

window.location.href = "/";
}

return Promise.reject(error);
},
);
Expand Down
1 change: 0 additions & 1 deletion src/hooks/api/auth/useLogout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const useLogout = () => {
onSuccess: () => {
removeLocalStorage(ACCESS_TOKEN);
removeLocalStorage(REFRESH_TOKEN);
location.reload();
},
});

Expand Down

0 comments on commit 7606f80

Please sign in to comment.