From 1c453b303a45f877209727d91e3c41c4a235c617 Mon Sep 17 00:00:00 2001 From: uxolrv Date: Thu, 6 Apr 2023 02:50:06 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20feat:=20useGet=EC=97=90=20?= =?UTF-8?q?=EC=A0=9C=EB=84=A4=EB=A6=AD=20=EC=A0=81=EC=9A=A9=20=EB=B0=8F=20?= =?UTF-8?q?403=20=EC=97=90=EB=9F=AC=20=EC=B2=98=EB=A6=AC=20#296?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/components/Etc/Constants.js | 3 +++ client/src/hooks/useFetch.ts | 25 +++++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/client/src/components/Etc/Constants.js b/client/src/components/Etc/Constants.js index 12be4ded..57acf6bf 100644 --- a/client/src/components/Etc/Constants.js +++ b/client/src/components/Etc/Constants.js @@ -121,3 +121,6 @@ export const ERROR_INFORMATION = `현재 정보를 불러올 수 없습니다. 잠시 후 다시 시도해주세요.`; export const NO_ORDER_HISTORY = '주문 내역이 없습니다.'; + +export const TOKEN_EXPIRED_INFOMATION = + '로그인이 만료되었습니다. 다시 로그인해주세요.'; diff --git a/client/src/hooks/useFetch.ts b/client/src/hooks/useFetch.ts index e6873b73..9ec4dd91 100644 --- a/client/src/hooks/useFetch.ts +++ b/client/src/hooks/useFetch.ts @@ -1,13 +1,26 @@ +import { useNavigate } from 'react-router-dom'; import { useMutation, useQuery, useQueryClient } from 'react-query'; import { useState } from 'react'; -import { AxiosResponse } from 'axios'; +import { AxiosResponse, AxiosError } from 'axios'; +import { toast } from 'react-toastify'; import axiosInstance from '../utils/axiosInstance'; +import { TOKEN_EXPIRED_INFOMATION } from '../components/Etc/Constants'; -export function useGet(url: string, keyValue: string) { - const { isLoading, isError, isSuccess, data, error, refetch } = useQuery( - [keyValue], - () => axiosInstance.get(url), - ); +export function useGet(url: string, keyValue: string) { + const navigate = useNavigate(); + const { isLoading, isError, isSuccess, data, error, refetch } = useQuery< + AxiosResponse + >([keyValue], () => axiosInstance.get(url), { + onError: (errRes) => { + const { response } = errRes as AxiosError; + + if (response?.status === 403) { + localStorage.clear(); + navigate('/login'); + toast.error(TOKEN_EXPIRED_INFOMATION); + } + }, + }); return { isLoading, isError, isSuccess, data, error, refetch }; }