Skip to content

Commit

Permalink
feat/#49/로그인하지 않은 상태로 마이페이지 접근 차단
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmj12 committed Jan 15, 2025
1 parent de60e25 commit 6d064c8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/components/Mypage/MypageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ type MypageLayoutProps = {
};

export default function MypageLayout({ children }: MypageLayoutProps) {
const { isLoggedIn } = useAuthStore();

if (!isLoggedIn) {
return null;
}

return (
<Styles.MypageContainer>
<Styles.MypageSidebarContainer>
Expand Down
24 changes: 24 additions & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,33 @@ import type { AppProps } from "next/app";
import localFont from "next/font/local";
import { ApolloProvider } from "@apollo/client";
import client from "@/lib/apolloClient";
import { useAuthStore } from "@/stores/authStore";
import { useRouter } from "next/router";
import { useEffect, useRef } from "react";
const myFont = localFont({ src: "../fonts/PretendardVariable.woff2" });

export default function App({ Component, pageProps }: AppProps) {
const { isLoggedIn } = useAuthStore();
const hasAlerted = useRef(false);
const router = useRouter();

// "/mypage는 로그인된 상태에서만 접근할 수 있음"
useEffect(() => {
const protectedRoutes = ["/mypage"];
const isProtectedRoute = protectedRoutes.some((route) =>
router.pathname.startsWith(route),
);

if (isProtectedRoute && !hasAlerted.current) {
hasAlerted.current = true;

if (!isLoggedIn) {
alert("로그인이 필요합니다");
router.replace("/");
}
}
}, [isLoggedIn, router]);

return (
<div className={myFont.className}>
<Layout>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default function Home() {
resetType();
}, [resetStack, resetType]);

const SIZE = 5;
const SIZE = 36;

const observerRef = useRef<HTMLDivElement>(null);
const { data, loading, fetchMore, refetch } = useQuery(SEARCH_PROJECT, {
Expand Down
14 changes: 3 additions & 11 deletions src/pages/mypage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { useAuthStore } from "@/stores/authStore";
import { useRouter } from "next/router";
import { useEffect, useRef } from "react";
import { useEffect } from "react";

export default function Mypage() {
const router = useRouter();
const { isLoggedIn } = useAuthStore();
const hasAlerted = useRef(false);

useEffect(() => {
if (!hasAlerted.current) {
hasAlerted.current = true;

if (isLoggedIn) {
router.replace("/mypage/myprojects");
} else {
alert("로그인이 필요합니다");
router.replace("/");
}
if (isLoggedIn) {
router.replace("/mypage/myprojects");
}
}, [isLoggedIn, router]);

Expand Down

0 comments on commit 6d064c8

Please sign in to comment.