Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat/CK-209] 새로고침 시 로그인 페이지로 이동하는 버그를 해결한다. #169

Merged
merged 2 commits into from
Oct 1, 2023
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
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;
Loading