Skip to content

Commit

Permalink
🐛 fix: 토큰 만료 상태에서 마이페이지 접근 시 로컬스토리지 클리어 및 로그인 페이지로 이동 #311
Browse files Browse the repository at this point in the history
  • Loading branch information
uxolrv committed Apr 16, 2023
1 parent 03ed3db commit 6536ff7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 4 additions & 1 deletion client/src/assets/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const ERROR_INFORMATION = `현재 정보를 불러올 수 없습니다.
export const NO_ORDER_HISTORY = '주문 내역이 없습니다.';

export const TOKEN_EXPIRED_INFORMATION =
'로그인이 만료되었습니다. 다시 로그인해주세요.';
'로그인 세션이 만료되었습니다. 다시 로그인해주세요.';

export const WRITE_MORE_THAN_20_CHARACTERS = '20자 이상 작성해주세요.';

Expand All @@ -154,3 +154,6 @@ export const NO_TALKS_WRITTEN = '작성하신 토크가 없습니다.';
export const NO_WISH_LIST = '찜한 상품이 없습니다.';

export const NO_SUBSCRIPTION_HISTORY = '정기구독 신청 내역이 없습니다.';

export const EXPIRED_TOKEN_RESPONSE_MESSAGE =
'Full authentication is required to access this resource';
16 changes: 14 additions & 2 deletions client/src/hooks/useFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import { useState } from 'react';
import { AxiosResponse, AxiosError } from 'axios';
import { toast } from 'react-toastify';
import axiosInstance from '../utils/axiosInstance';
import { TOKEN_EXPIRED_INFORMATION } from '../assets/Constants';
import {
TOKEN_EXPIRED_INFORMATION,
EXPIRED_TOKEN_RESPONSE_MESSAGE,
} from '../assets/Constants';

interface ResponseData {
message: string;
status: number;
}

export function useGet<T>(url: string, keyValue: string) {
const navigate = useNavigate();
Expand All @@ -13,8 +21,12 @@ export function useGet<T>(url: string, keyValue: string) {
>([keyValue], () => axiosInstance.get(url), {
onError: (errRes) => {
const { response } = errRes as AxiosError;
const { message } = response?.data as ResponseData;

if (response?.status === 403) {
if (
response?.status === 403 ||
message === EXPIRED_TOKEN_RESPONSE_MESSAGE
) {
localStorage.clear();
navigate('/login');
toast.error(TOKEN_EXPIRED_INFORMATION);
Expand Down

0 comments on commit 6536ff7

Please sign in to comment.