Skip to content

Commit

Permalink
Merge pull request #169 from woowacourse-teams/feature/CK-209
Browse files Browse the repository at this point in the history
[feat/CK-209] ์ƒˆ๋กœ๊ณ ์นจ ์‹œ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋Š” ๋ฒ„๊ทธ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.
  • Loading branch information
sh981013s authored Oct 1, 2023
2 parents 1154295 + 908b3e5 commit ce9c5c7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
26 changes: 4 additions & 22 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { lazy, PropsWithChildren, Suspense, useEffect } from 'react';
import { BrowserRouter, Route, Routes, useNavigate } from 'react-router-dom';
import { lazy, Suspense } from 'react';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import theme from '@styles/theme';
import GlobalStyle from '@styles/GlobalStyle';
import { ThemeProvider } from 'styled-components';
Expand All @@ -13,15 +13,13 @@ import RoadmapDetailPage from './pages/roadmapDetailPage/RoadmapDetailPage';
import RoadmapCreatePage from './pages/roadmapCreatePage/RoadmapCreatePage';
import ToastProvider from '@components/_common/toastProvider/ToastProvider';
import MyPage from '@pages/myPage/MyPage';
import UserInfoProvider, {
useUserInfoContext,
} from './components/_providers/UserInfoProvider';
import UserInfoProvider from './components/_providers/UserInfoProvider';
import RoadmapSearchResult from './components/roadmapListPage/roadmapSearch/RoadmapSearchResult';
import MainPage from '@pages/mainPage/MainPage';
import useToast from '@hooks/_common/useToast';
import OAuthRedirect from './components/loginPage/OAuthRedirect';
import AsyncBoundary from './components/_common/errorBoundary/AsyncBoundary';
import SessionHandler from '@components/_common/sessionHandler/SessionHandler';
import PrivateRouter from '@components/_common/privateRouter/PrivateRouter';

const GoalRoomDashboardPage = lazy(
() => import('@pages/goalRoomDashboardPage/GoalRoomDashboardPage')
Expand All @@ -31,22 +29,6 @@ const GoalRoomCreatePage = lazy(
() => import('@pages/goalRoomCreatePage/GoalRoomCreatePage')
);

const PrivateRouter = (props: PropsWithChildren) => {
const { children } = props;
const { userInfo } = useUserInfoContext();
const { triggerToast } = useToast();
const navigate = useNavigate();

useEffect(() => {
if (userInfo.id === null) {
navigate('/login');
triggerToast({ message: '๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•œ ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค.' });
}
}, [userInfo.id, navigate]);

return <>{children}</>;
};

const App = () => {
return (
<ThemeProvider theme={theme}>
Expand Down
24 changes: 24 additions & 0 deletions client/src/components/_common/privateRouter/PrivateRouter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { PropsWithChildren, useEffect } from 'react';
import { useUserInfoContext } from '@components/_providers/UserInfoProvider';
import useToast from '@hooks/_common/useToast';
import { useNavigate } from 'react-router-dom';
import { getCookie } from '@utils/_common/cookies';

const PrivateRouter = (props: PropsWithChildren) => {
const { children } = props;
const { userInfo } = useUserInfoContext();
const { triggerToast } = useToast();
const navigate = useNavigate();
const accessToken = getCookie('access_token');

useEffect(() => {
if (userInfo.id === null && !accessToken) {
navigate('/login');
triggerToast({ message: '๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•œ ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค.' });
}
}, [userInfo.id, navigate]);

return <>{children}</>;
};

export default PrivateRouter;

0 comments on commit ce9c5c7

Please sign in to comment.