Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] private, public route 설정 #215

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions packages/frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { RouterProvider } from 'react-router-dom';
import { router } from './routes/router';
import { useEffect } from 'react';

const queryClient = new QueryClient();

function App() {
useEffect(() => {
const handleUnload = () => {
Expand All @@ -21,9 +18,7 @@ function App() {

return (
<>
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
</QueryClientProvider>
<RouterProvider router={router} />
</>
);
}
Expand Down
16 changes: 16 additions & 0 deletions packages/frontend/src/components/PrivateRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Navigate } from 'react-router-dom';
import { useAuthStore } from '@/store/authStore';

interface IPrivateRouteProps {
children: React.ReactNode;
}

export default function PrivateRoute({ children }: IPrivateRouteProps) {
const { userId } = useAuthStore();

if (!userId) {
return <Navigate to="/" replace />;
}

return <>{children}</>;
}
16 changes: 16 additions & 0 deletions packages/frontend/src/components/PublicRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Navigate } from 'react-router-dom';
import { useAuthStore } from '@/store/authStore';

interface IPublicRouteProps {
children: React.ReactNode;
}

export default function PublicRoute({ children }: IPublicRouteProps) {
const { userId } = useAuthStore();

if (userId) {
return <Navigate to="/lobby" replace />;
}

return <>{children}</>;
}
8 changes: 0 additions & 8 deletions packages/frontend/src/pages/gamePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@ import { useState } from 'react';
import BackgroundImage from '@/components/layout/BackgroundImage';
import LeftGameSection from '@/components/gamePage/leftSection/LeftGameSection';
import RightGameSection from '@/components/gamePage/rightSection/RightGameSection';
import Header from '@/components/layout/Header';
import GameManual from '@/components/gamePage/GameManual';
import { useSocketStore } from '@/store/socketStore';
import { useRoomStore } from '@/store/roomStore';

export default function GamePage() {
const { socket } = useSocketStore();
console.log('gamePage', socket?.connected);
const { allUsers } = useRoomStore();
console.log('방 내에 모든 allUsers 목록', allUsers);

const [isGameManualVisible, setIsGameManualVisible] = useState(true);

const handleCloseManual = () => {
Expand Down
3 changes: 0 additions & 3 deletions packages/frontend/src/pages/landingPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import GuestLoginButton from '@/components/landingPage/GuestLoginButton';
import BackgroundImage from '@/components/layout/BackgroundImage';
import MainLogo from '@/assets/images/MainLogo.svg?react';
import { useSocketStore } from '@/store/socketStore';

export default function LandingPage() {
const { socket } = useSocketStore();
console.log('landingPage', socket?.connected);
return (
<>
<BackgroundImage gradientClass="bg-gradient-to-t from-black/90" />
Expand Down
2 changes: 0 additions & 2 deletions packages/frontend/src/pages/lobbyPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
setChatHistory([]);
setRoomData(null, false, false);
}
}, []);

Check warning on line 27 in packages/frontend/src/pages/lobbyPage/index.tsx

View workflow job for this annotation

GitHub Actions / Frontend CI

React Hook useEffect has missing dependencies: 'gsid', 'setChatHistory', 'setRoomData', and 'socket'. Either include them or remove the dependency array

Check warning on line 27 in packages/frontend/src/pages/lobbyPage/index.tsx

View workflow job for this annotation

GitHub Actions / Frontend CI

React Hook useEffect has missing dependencies: 'gsid', 'setChatHistory', 'setRoomData', and 'socket'. Either include them or remove the dependency array

console.log('lobbyPage', socket?.connected);

return (
<>
<BackgroundImage gradientClass="bg-gradient-to-t from-black/90" />
Expand Down
20 changes: 17 additions & 3 deletions packages/frontend/src/routes/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Layout from '@/components/layout/Layout';
import LandingPage from '@/pages/landingPage/index';
import LobbyPage from '@/pages/lobbyPage/index';
import GamePage from '@/pages/gamePage/index';
import PublicRoute from '@/components/PublicRoute';
import PrivateRoute from '@/components/PrivateRoute';

export const router = createBrowserRouter([
{
Expand All @@ -11,15 +13,27 @@ export const router = createBrowserRouter([
children: [
{
index: true,
element: <LandingPage />,
element: (
<PublicRoute>
<LandingPage />
</PublicRoute>
),
},
{
path: 'lobby',
element: <LobbyPage />,
element: (
<PrivateRoute>
<LobbyPage />
</PrivateRoute>
),
},
{
path: 'game/:gsid',
element: <GamePage />,
element: (
<PrivateRoute>
<GamePage />
</PrivateRoute>
),
},
],
},
Expand Down