diff --git a/frontend/src/pages/auth/Redirection.tsx b/frontend/src/pages/auth/Redirection.tsx
index 31db378c4..4210131b7 100644
--- a/frontend/src/pages/auth/Redirection.tsx
+++ b/frontend/src/pages/auth/Redirection.tsx
@@ -5,6 +5,10 @@ import { AuthResponse } from '@type/auth';
import { AuthContext } from '@hooks/context/auth';
+import Error from '@pages/Error';
+
+import LoadingSpinner from '@components/common/LoadingSpinner';
+
import { getCookieToken, setCookieToken } from '@utils/cookie';
import { getFetch } from '@utils/fetch';
@@ -36,7 +40,9 @@ export default function Redirection() {
setErrorMessage(error.message);
})
.then(res => {
- if (!res) return setErrorMessage('잘못된 형식의 response');
+ if (!res) {
+ return setErrorMessage('로그인 중 오류가 발생했습니다.');
+ }
const { accessToken } = res;
setCookieToken('accessToken', accessToken);
@@ -50,12 +56,14 @@ export default function Redirection() {
navigate('/');
});
})();
- }, [navigate, loggedInfo, setLoggedInfo]);
-
- return (
-
- {isLoading && '로그인 중입니다...'}
- {errorMessage && errorMessage}
-
- );
+ }, [navigate, loggedInfo, setLoggedInfo, params]);
+
+ if (isLoading)
+ return (
+
+
+
+ );
+
+ if (errorMessage) return ;
}
diff --git a/frontend/src/routes/PrivateRoute.tsx b/frontend/src/routes/PrivateRoute.tsx
new file mode 100644
index 000000000..c439a7506
--- /dev/null
+++ b/frontend/src/routes/PrivateRoute.tsx
@@ -0,0 +1,31 @@
+import { PropsWithChildren } from 'react';
+import { Navigate } from 'react-router-dom';
+
+import { PATH } from '@constants/path';
+
+import { getCookieToken } from '@utils/cookie';
+
+interface Route extends PropsWithChildren {
+ isAuthenticated?: boolean;
+ path?: (typeof PATH)[keyof typeof PATH];
+}
+
+const PrivateRoute = ({ children, isAuthenticated = true, path = PATH.LOGIN }: Route) => {
+ const isLoggedIn = getCookieToken().accessToken;
+
+ if (!isLoggedIn) {
+ alert('해당 페이지에 접근하려면 로그인이 필요합니다.');
+
+ return ;
+ }
+
+ if (!isAuthenticated) {
+ alert('해당 페이지에 대한 접근 권한이 없습니다.');
+
+ return ;
+ }
+
+ return children;
+};
+
+export default PrivateRoute;
diff --git a/frontend/src/routes/router.tsx b/frontend/src/routes/router.tsx
index f35ba5ff6..5cf86b232 100644
--- a/frontend/src/routes/router.tsx
+++ b/frontend/src/routes/router.tsx
@@ -4,6 +4,7 @@ import Login from '@pages/auth/Login';
import Redirection from '@pages/auth/Redirection';
import Home from '@pages/Home';
import MyInfo from '@pages/MyInfo';
+import NotFound from '@pages/NotFound';
import CreatePost from '@pages/post/CreatePost';
import EditPost from '@pages/post/EditPost';
import PostDetailPage from '@pages/post/PostDetail';
@@ -11,6 +12,8 @@ import VoteStatisticsPage from '@pages/VoteStatistics';
import { PATH } from '@constants/path';
+import PrivateRoute from './PrivateRoute';
+
const router = createBrowserRouter([
{
path: PATH.HOME,
@@ -28,10 +31,21 @@ const router = createBrowserRouter([
{
path: PATH.POST,
children: [
- { path: 'write', element: },
+ {
+ path: 'write',
+ element: (
+
+
+
+ ),
+ },
{
path: 'write/:postId',
- element: ,
+ element: (
+
+
+
+ ),
},
{
path: ':postId',
@@ -39,7 +53,11 @@ const router = createBrowserRouter([
},
{
path: 'result/:postId',
- element: ,
+ element: (
+
+
+
+ ),
},
{ path: 'category/:categoryId', element: },
],
@@ -47,10 +65,21 @@ const router = createBrowserRouter([
{
path: PATH.USER,
children: [
- { path: 'myPage', element: },
+ {
+ path: 'myPage',
+ element: (
+
+
+
+ ),
+ },
{ path: 'posts', element: },
{ path: 'votes', element: },
],
},
+ {
+ path: '*',
+ element: ,
+ },
]);
export default router;
diff --git a/frontend/src/utils/post/checkWriter.ts b/frontend/src/utils/post/checkWriter.ts
new file mode 100644
index 000000000..fba8ebb98
--- /dev/null
+++ b/frontend/src/utils/post/checkWriter.ts
@@ -0,0 +1,8 @@
+import { getCookieToken, getMemberId } from '@utils/cookie';
+
+export function checkWriter(writedId: number) {
+ const accessToken = getCookieToken().accessToken;
+ const memberId = getMemberId(accessToken).memberId;
+
+ return writedId === memberId;
+}