Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Header from '@/shared/components/header/Header';
import FooterWrapper from '@/shared/components/footer/FooterWrapper';
import ScrollTopBtnWrapper from '@/shared/components/scroll-top/ScrollTopBtnWrapper';
import KaKaoScript from './api/kakao/KaKaoScript';
import IdleHandler from '@/domains/login/components/IdleHandler';
import Provider from '@/shared/api/Provider';
import ClientInitHook from '@/domains/login/components/ClientInitHook';

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

Expand Down
11 changes: 11 additions & 0 deletions src/domains/login/components/ClientInitHook.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use client';

import { useFetchInterceptor } from '@/shared/hook/useFetchInterceptor';
import { useIdleLogout } from '../hook/useIdleLogout';

function ClientInitHook() {
useIdleLogout();
useFetchInterceptor();
return null;
}
export default ClientInitHook;
9 changes: 0 additions & 9 deletions src/domains/login/components/IdleHandler.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/domains/recipe/components/main/Cocktails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import CocktailSearchBar from './CocktailSearchBar';
import useSearchControl from '../../hook/useSearchControl';
import CocktailSearch from '../../api/CocktailSearch';


function Cocktails() {
const [data, setData] = useState<Cocktail[]>([]);
const [lastId, setLastId] = useState<number | null>(null);
Expand Down
43 changes: 43 additions & 0 deletions src/shared/hook/useFetchInterceptor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use client';

import { useEffect } from 'react';
import { getApi } from '@/app/api/config/appConfig';
import { useRouter } from 'next/navigation';
import { useToast } from '@/shared/hook/useToast';

export const useFetchInterceptor = () => {
const router = useRouter();
const { toastInfo } = useToast();

useEffect(() => {
const originalFetch = global.fetch;

(global.fetch as typeof global.fetch) = async (input, init?) => {
const response = await originalFetch(input, { ...init, credentials: 'include' });

if (response.status === 401) {
try {
const refreshRes = await originalFetch(`${getApi}/user/auth/refresh`, {
method: 'POST',
credentials: 'include',
});

if (refreshRes.ok) {
return originalFetch(input, { ...init, credentials: 'include' });
} else {
toastInfo('로그인 인증 만료로 다시 로그인해주세요.');
router.push('/login');
}
} catch {
toastInfo('로그인 인증 만료로 다시 로그인해주세요.');
router.push('/login');
}
}
return response;
};

return () => {
global.fetch = originalFetch;
};
}, [router, toastInfo]);
};