diff --git a/src/Router.tsx b/src/Router.tsx index 8ad6606..0a8b5d1 100644 --- a/src/Router.tsx +++ b/src/Router.tsx @@ -7,6 +7,7 @@ import Game from './pages/Game'; import InputCode from './pages/InputCode'; import InputName from './pages/InputName'; import NotFound from './pages/NotFound'; +import CheckAuth from './routers/CheckAuth'; import CheckInAppBrowser from './routers/CheckInAppBrowser'; interface RouteElement { @@ -19,27 +20,39 @@ interface RouteElement { const routes: RouteElement[] = [ { path: '/', - element: , + element: ( + + + + ), }, { path: 'create', element: ( - - - + + + + + ), }, { path: '/participate', element: ( - - - + + + + + ), }, { path: '/name', - element: , + element: ( + + + + ), }, { path: '/game', diff --git a/src/axios/http.ts b/src/axios/http.ts index 8a60846..c616463 100644 --- a/src/axios/http.ts +++ b/src/axios/http.ts @@ -4,6 +4,7 @@ import { Chat, ChatRequest, DeadResult, + GameExist, GameInfo, GamesResults, MafiaVoteResult, @@ -89,6 +90,11 @@ export const startGame = async () => { export const getRoomsResults = () => { return http.get('/games/result'); }; + +export const existGame = () => { + return http.get('/games/valid'); +}; + export const getChats = () => { return http.get(`/chat`); }; diff --git a/src/hooks/useInAppBrowser b/src/hooks/useInAppBrowser deleted file mode 100644 index e69de29..0000000 diff --git a/src/pages/InputName.tsx b/src/pages/InputName.tsx index c2c8600..a54097f 100644 --- a/src/pages/InputName.tsx +++ b/src/pages/InputName.tsx @@ -12,6 +12,7 @@ import TopEnter from '../components/top/TopEnter'; import { VariablesCSS } from '../styles/VariablesCSS'; export default function InputName() { + const navigate = useNavigate(); const [name, setName] = useState(''); const onName = (e: React.ChangeEvent) => { if (e.target.value.length <= 10) { @@ -27,7 +28,6 @@ export default function InputName() { const [searchParams] = useSearchParams(); const code = searchParams.get('code') as string; - const navigate = useNavigate(); const goWaitingRoom = async (event: FormEvent) => { event.preventDefault(); diff --git a/src/routers/CheckAuth.tsx b/src/routers/CheckAuth.tsx new file mode 100644 index 0000000..dea91e8 --- /dev/null +++ b/src/routers/CheckAuth.tsx @@ -0,0 +1,24 @@ +import { useEffect } from 'react'; +import { useNavigate } from 'react-router-dom'; + +import { existGame } from '../axios/http'; + +interface propsType { + children: React.ReactNode; +} + +export default function CheckAuth({ children }: propsType) { + const navigate = useNavigate(); + useEffect(() => { + (async () => { + const isExist = await existGame(); + if (isExist) { + if (confirm('이미 참가중인 방이 있습니다. 재접속 하시겠습니까?')) { + navigate('/game'); + } + } + })(); + }, []); + + return <>{children}; +} diff --git a/src/type/index.ts b/src/type/index.ts index 8780315..184a267 100644 --- a/src/type/index.ts +++ b/src/type/index.ts @@ -105,3 +105,7 @@ export interface GamesResults { winner: Player[]; loser: Player[]; } + +export interface GameExist { + valid: boolean; +}