Skip to content

Commit 4f91a4f

Browse files
authored
[feat] 401 응답처리 로직 기능 (#124)
* [feat] fetch interceptor 훅 * [fix] format 오류 해결
1 parent 8bb232d commit 4f91a4f

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed

src/app/layout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import Header from '@/shared/components/header/Header';
55
import FooterWrapper from '@/shared/components/footer/FooterWrapper';
66
import ScrollTopBtnWrapper from '@/shared/components/scroll-top/ScrollTopBtnWrapper';
77
import KaKaoScript from './api/kakao/KaKaoScript';
8-
import IdleHandler from '@/domains/login/components/IdleHandler';
98
import Provider from '@/shared/api/Provider';
9+
import ClientInitHook from '@/domains/login/components/ClientInitHook';
1010

1111
export const metadata: Metadata = {
1212
title: { default: 'SSOUL', template: 'SSOUL | %s' },
@@ -24,7 +24,7 @@ export default function RootLayout({
2424
<body className="relative flex flex-col min-h-screen">
2525
<Provider>
2626
<Header />
27-
<IdleHandler />
27+
<ClientInitHook />
2828
<main className="flex flex-1 pt-[2.75rem] md:pt-[3.75rem]">{children}</main>
2929
<FooterWrapper />
3030

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use client';
2+
3+
import { useFetchInterceptor } from '@/shared/hook/useFetchInterceptor';
4+
import { useIdleLogout } from '../hook/useIdleLogout';
5+
6+
function ClientInitHook() {
7+
useIdleLogout();
8+
useFetchInterceptor();
9+
return null;
10+
}
11+
export default ClientInitHook;

src/domains/login/components/IdleHandler.tsx

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/domains/recipe/components/main/Cocktails.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import CocktailSearchBar from './CocktailSearchBar';
1010
import useSearchControl from '../../hook/useSearchControl';
1111
import CocktailSearch from '../../api/CocktailSearch';
1212

13-
1413
function Cocktails() {
1514
const [data, setData] = useState<Cocktail[]>([]);
1615
const [lastId, setLastId] = useState<number | null>(null);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use client';
2+
3+
import { useEffect } from 'react';
4+
import { getApi } from '@/app/api/config/appConfig';
5+
import { useRouter } from 'next/navigation';
6+
import { useToast } from '@/shared/hook/useToast';
7+
8+
export const useFetchInterceptor = () => {
9+
const router = useRouter();
10+
const { toastInfo } = useToast();
11+
12+
useEffect(() => {
13+
const originalFetch = global.fetch;
14+
15+
(global.fetch as typeof global.fetch) = async (input, init?) => {
16+
const response = await originalFetch(input, { ...init, credentials: 'include' });
17+
18+
if (response.status === 401) {
19+
try {
20+
const refreshRes = await originalFetch(`${getApi}/user/auth/refresh`, {
21+
method: 'POST',
22+
credentials: 'include',
23+
});
24+
25+
if (refreshRes.ok) {
26+
return originalFetch(input, { ...init, credentials: 'include' });
27+
} else {
28+
toastInfo('로그인 인증 만료로 다시 로그인해주세요.');
29+
router.push('/login');
30+
}
31+
} catch {
32+
toastInfo('로그인 인증 만료로 다시 로그인해주세요.');
33+
router.push('/login');
34+
}
35+
}
36+
return response;
37+
};
38+
39+
return () => {
40+
global.fetch = originalFetch;
41+
};
42+
}, [router, toastInfo]);
43+
};

0 commit comments

Comments
 (0)