Skip to content

Commit

Permalink
feat: 비로그인상태에서 privateRoute에 접근했을 때 해당 path를 저장 후 로그인하면 바로 해당 path로 이…
Browse files Browse the repository at this point in the history
…동 기능 추가
  • Loading branch information
dabin-Hailey committed Jan 19, 2024
1 parent 07767a5 commit b73cb96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/components/Login/LoginForm/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -13,6 +13,7 @@ import { setCookies } from '@utils/lib/cookies';

const LoginForm = () => {
const navigate = useNavigate();
const { state } = useLocation();

// HACK : 추후 useState로 입력 데이터 관리할 예쩡
const formData: LoginData = {
Expand All @@ -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 (
Expand Down
19 changes: 13 additions & 6 deletions src/routes/PrivateRouter/index.tsx
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -12,11 +16,14 @@ const PrivateRouter = () => {
const isLoggedIn =
!!accessToken && !!refreshToken && !!userName && !!userEmail;

if (!isLoggedIn) {
// HACK : alert창은 추후 변경 예정입니다.
alert('로그인이 필요한 기능입니다.');
return <Navigate to="/login" />;
}
useEffect(() => {
if (!isLoggedIn) {
// HACK : alert창은 추후 변경 예정입니다.
alert('로그인이 필요한 기능입니다.');
navigate('/login', { state: pathname });
}
}, []);

return <Layout />;
};

Expand Down

0 comments on commit b73cb96

Please sign in to comment.