Skip to content

Commit

Permalink
feat: 로그인 토큰 인증이 새로고침시에도 동작하도록 최상위 컴포넌트에서 실행 #179
Browse files Browse the repository at this point in the history
  • Loading branch information
seoye0ng committed Feb 20, 2024
1 parent 05dec8e commit fe575c1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ModalContextProvider } from '@contexts/ModalContext';
import { CookieProvider } from '@providers/CookieProvider';
import MockProvider from '@providers/MockProvider';
import QueryProvider from '@providers/QueryProvider';
import RefreshTokenProvider from '@providers/RefreshTokenProvider';
import StoreProvider from '@providers/StoreProvider';
// eslint-disable-next-line import/order
// import { CookiesProvider } from 'react-cookie';
Expand Down Expand Up @@ -40,7 +41,9 @@ export default function RootLayout({
<QueryProvider>
<StoreProvider>
<ModalContextProvider>
{children}
<RefreshTokenProvider>
{children}
</RefreshTokenProvider>
</ModalContextProvider>
</StoreProvider>
</QueryProvider>
Expand Down
31 changes: 31 additions & 0 deletions src/providers/RefreshTokenProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable react/jsx-no-useless-fragment */
/* eslint-disable no-console */

'use client';

import { useEffect } from 'react';

import useIntervalRefreshToken from '@hooks/useIntervalRefreshToken';
import { useAppSelector } from '@stores/hooks';

function RefreshTokenProvider({ children }: { children: React.ReactNode }) {
const { startRefreshTokenInterval, refreshTokenClear } = useIntervalRefreshToken();
const userId = useAppSelector((state) => { return state.user.id; });

useEffect(() => {
if (userId !== null) {
startRefreshTokenInterval();
console.log('RefreshTokenProvider mounted');
}

// cleanup 함수를 이용하여 컴포넌트가 unmount 될 때 clearInterval 호출
return () => {
refreshTokenClear();
console.log('RefreshTokenProvider unmounted');
};
}, [userId]);

return <>{children}</>;
}

export default RefreshTokenProvider;

0 comments on commit fe575c1

Please sign in to comment.