diff --git a/src/components/Login/LoginForm/index.tsx b/src/components/Login/LoginForm/index.tsx index e4ae9a76..02b6ceb2 100644 --- a/src/components/Login/LoginForm/index.tsx +++ b/src/components/Login/LoginForm/index.tsx @@ -1,4 +1,4 @@ -import { useNavigate } from 'react-router-dom'; +import { useLocation, useNavigate } from 'react-router-dom'; import styled from '@emotion/styled'; import { InputValidation } from '@/types/login'; @@ -13,6 +13,7 @@ import { setCookies } from '@utils/lib/cookies'; const LoginForm = () => { const navigate = useNavigate(); + const { state } = useLocation(); // HACK : 추후 useState로 입력 데이터 관리할 예쩡 const formData: LoginData = { @@ -37,6 +38,12 @@ const LoginForm = () => { setCookies('userEmail', response.email, response.expires_in); setCookies('accessToken', response.access_token, response.expires_in); setCookies('refreshToken', response.refresh_token, response.expires_in); + + if (state) { + navigate(state); + } else { + navigate('/'); + } }; return ( diff --git a/src/routes/PrivateRouter/index.tsx b/src/routes/PrivateRouter/index.tsx index 1ad92ddb..70405805 100644 --- a/src/routes/PrivateRouter/index.tsx +++ b/src/routes/PrivateRouter/index.tsx @@ -1,9 +1,13 @@ -import { Navigate } from 'react-router-dom'; +import { useEffect } from 'react'; +import { useLocation, useNavigate } from 'react-router-dom'; import { Layout } from '@components/common'; import { getCookies } from '@utils/lib/cookies'; const PrivateRouter = () => { + const navigate = useNavigate(); + const pathname = useLocation(); + const accessToken = getCookies('accessToken'); const refreshToken = getCookies('refreshToken'); const userName = getCookies('userName'); @@ -12,11 +16,14 @@ const PrivateRouter = () => { const isLoggedIn = !!accessToken && !!refreshToken && !!userName && !!userEmail; - if (!isLoggedIn) { - // HACK : alert창은 추후 변경 예정입니다. - alert('로그인이 필요한 기능입니다.'); - return ; - } + useEffect(() => { + if (!isLoggedIn) { + // HACK : alert창은 추후 변경 예정입니다. + alert('로그인이 필요한 기능입니다.'); + navigate('/login', { state: pathname }); + } + }, []); + return ; };