From 904ad51a6202a4668b16513e830cc62f2fd48ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B3=B4=EB=AF=BC?= Date: Tue, 23 Jul 2024 17:51:09 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EB=A1=9C=EA=B7=B8=EC=95=84=EC=9B=83=20?= =?UTF-8?q?=EC=83=81=ED=83=9C=EC=97=90=EC=84=9C=20=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=80=20=EC=A0=91=EA=B7=BC=EC=8B=9C=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=ED=8E=98=EC=9D=B4=EC=A7=80=EB=A1=9C=20=EB=A6=AC?= =?UTF-8?q?=EB=8B=A4=EC=9D=B4=EB=A0=89=ED=8A=B8=20=ED=95=98=EB=8A=94=20=20?= =?UTF-8?q?=EC=BB=A4=EC=8A=A4=ED=85=80=20=ED=9B=85=20=EA=B5=AC=ED=98=84=20?= =?UTF-8?q?=EB=B0=8F=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hooks/useAuthRedirect.ts | 15 +++++++++++++++ pages/calendar.tsx | 3 +++ pages/myactivity/edit.tsx | 3 +++ pages/myactivity/index.tsx | 3 +++ pages/myactivity/register.tsx | 4 ++++ pages/mypage.tsx | 13 +++---------- pages/reservation.tsx | 3 +++ 7 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 hooks/useAuthRedirect.ts diff --git a/hooks/useAuthRedirect.ts b/hooks/useAuthRedirect.ts new file mode 100644 index 0000000..9694417 --- /dev/null +++ b/hooks/useAuthRedirect.ts @@ -0,0 +1,15 @@ +import { useRouter } from 'next/router'; +import { useEffect } from 'react'; +import { usePopup } from './usePopup'; +import useLoginState from './useLoginState'; + +export default function useAuthRedirect() { + const router = useRouter(); + + useEffect(() => { + const refreshToken = localStorage.getItem('refreshToken'); + if (!refreshToken) { + router.push('/login'); + } + }, [router]); +} diff --git a/pages/calendar.tsx b/pages/calendar.tsx index 100d905..6a3bada 100644 --- a/pages/calendar.tsx +++ b/pages/calendar.tsx @@ -16,6 +16,7 @@ import { darkModeState } from '@/states/themeState'; import { InitialPageMeta } from '@/components/MetaData/MetaData'; import { GetServerSideProps } from 'next'; import { SSRMetaProps } from '@/components/MetaData/MetaData.type'; +import useAuthRedirect from '@/hooks/useAuthRedirect'; export const getServerSideProps: GetServerSideProps = async () => { const OGTitle = '예약현황 | GLOBALNOMAD'; @@ -32,6 +33,8 @@ export default function CalendarPage({ OGTitle, OGUrl }: SSRMetaProps) { const [activityId, setActivityId] = useState(null); const [isOpen, setIsOpen] = useRecoilState(sideNavigationState); const [isDarkMode, setIsDarkMode] = useRecoilState(darkModeState); + // 로그아웃 상태에서 페이지 접근시 로그인 페이지로 redirect + useAuthRedirect(); const openSideNavigation = () => { setIsOpen(!isOpen); diff --git a/pages/myactivity/edit.tsx b/pages/myactivity/edit.tsx index ef4285c..bb229f2 100644 --- a/pages/myactivity/edit.tsx +++ b/pages/myactivity/edit.tsx @@ -6,6 +6,7 @@ import { getActivityInfo } from '../api/activities/apiactivities'; import { GetServerSideProps } from 'next'; import { InitialPageMeta } from '@/components/MetaData/MetaData'; import { SSRMetaProps } from '@/components/MetaData/MetaData.type'; +import useAuthRedirect from '@/hooks/useAuthRedirect'; export const getServerSideProps: GetServerSideProps = async () => { const OGTitle = '내 체험 수정 | GLOBALNOMAD'; @@ -20,6 +21,8 @@ export const getServerSideProps: GetServerSideProps = async () => { function EditMyActivity({ OGTitle, OGUrl }: SSRMetaProps) { const { query } = useRouter(); + // 로그아웃 상태에서 페이지 접근시 로그인 페이지로 redirect + useAuthRedirect(); const id = Number(query.activityId); const { data: activityData } = useQuery({ queryKey: ['activityDetailsEdit', id], diff --git a/pages/myactivity/index.tsx b/pages/myactivity/index.tsx index c415b27..b79df71 100644 --- a/pages/myactivity/index.tsx +++ b/pages/myactivity/index.tsx @@ -15,6 +15,7 @@ import { darkModeState } from '@/states/themeState'; import { InitialPageMeta } from '@/components/MetaData/MetaData'; import { GetServerSideProps } from 'next'; import { SSRMetaProps } from '@/components/MetaData/MetaData.type'; +import useAuthRedirect from '@/hooks/useAuthRedirect'; export const getServerSideProps: GetServerSideProps = async () => { const OGTitle = '내 체험 관리 | GLOBALNOMAD'; @@ -34,6 +35,8 @@ function MyActivity({ OGTitle, OGUrl }: SSRMetaProps) { useMyActivityList(); const [isOpen, setIsOpen] = useRecoilState(sideNavigationState); const [isDarkMode, setIsDarkMode] = useRecoilState(darkModeState); + // 로그아웃 상태에서 페이지 접근시 로그인 페이지로 redirect + useAuthRedirect(); const openSideNavigation = () => { setIsOpen(!isOpen); diff --git a/pages/myactivity/register.tsx b/pages/myactivity/register.tsx index b1056ff..5806dd3 100644 --- a/pages/myactivity/register.tsx +++ b/pages/myactivity/register.tsx @@ -1,6 +1,7 @@ import { InitialPageMeta } from '@/components/MetaData/MetaData'; import { SSRMetaProps } from '@/components/MetaData/MetaData.type'; import RegisterForm from '@/components/MyActivity/Register/RegisterForm'; +import useAuthRedirect from '@/hooks/useAuthRedirect'; import { GetServerSideProps } from 'next'; export const getServerSideProps: GetServerSideProps = async () => { @@ -15,6 +16,9 @@ export const getServerSideProps: GetServerSideProps = async () => { }; function RegisterActivity({ OGTitle, OGUrl }: SSRMetaProps) { + // 로그아웃 상태에서 페이지 접근시 로그인 페이지로 redirect + useAuthRedirect(); + return ( <> diff --git a/pages/mypage.tsx b/pages/mypage.tsx index 128dd0c..9c636e3 100644 --- a/pages/mypage.tsx +++ b/pages/mypage.tsx @@ -3,6 +3,7 @@ import { SSRMetaProps } from '@/components/MetaData/MetaData.type'; import MyPageInput from '@/components/MyPageInput/MyPageInput'; import SidenNavigation from '@/components/SideNavigation/SideNavigation'; import SidenNavigationMobile from '@/components/SideNavigation/SideNavigationMobile'; +import useAuthRedirect from '@/hooks/useAuthRedirect'; import useLoginState from '@/hooks/useLoginState'; import { useSideNavigation } from '@/hooks/useSideNavigation'; import { GetServerSideProps } from 'next'; @@ -24,16 +25,8 @@ export default function MyPage({ OGTitle, OGUrl }: SSRMetaProps) { const route = useRouter(); const { isLoggedIn } = useLoginState(); const { isOpen } = useSideNavigation(); - - useEffect(() => { - if (!isLoggedIn) { - route.push('/login'); - } - }, [isLoggedIn, route]); - - if (!isLoggedIn) { - return null; - } + // 로그아웃 상태에서 페이지 접근시 로그인 페이지로 redirect + useAuthRedirect(); return ( <> diff --git a/pages/reservation.tsx b/pages/reservation.tsx index 7b917d5..744cb0a 100644 --- a/pages/reservation.tsx +++ b/pages/reservation.tsx @@ -18,6 +18,7 @@ import { darkModeState } from '@/states/themeState'; import { InitialPageMeta } from '@/components/MetaData/MetaData'; import { GetServerSideProps } from 'next'; import { SSRMetaProps } from '@/components/MetaData/MetaData.type'; +import useAuthRedirect from '@/hooks/useAuthRedirect'; export const getServerSideProps: GetServerSideProps = async () => { const OGTitle = '예약내역 | GLOBALNOMAD'; @@ -40,6 +41,8 @@ export default function MyReservationPage({ OGTitle, OGUrl }: SSRMetaProps) { useReservationList(filterOption); const [isOpen, setIsOpen] = useRecoilState(sideNavigationState); const [isDarkMode, setIsDarkMode] = useRecoilState(darkModeState); + // 로그아웃 상태에서 페이지 접근시 로그인 페이지로 redirect + useAuthRedirect(); const openSideNavigation = () => { setIsOpen(!isOpen);