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: localStroage에 있는 auth가 유효하면 재접속할 수 있도록 comfirm 띄우기 #121

Merged
merged 2 commits into from
Sep 8, 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
29 changes: 21 additions & 8 deletions src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -19,27 +20,39 @@ interface RouteElement {
const routes: RouteElement[] = [
{
path: '/',
element: <FirstPage />,
element: (
<CheckAuth>
<FirstPage />
</CheckAuth>
),
},
{
path: 'create',
element: (
<CheckInAppBrowser>
<CreateRoom />
</CheckInAppBrowser>
<CheckAuth>
<CheckInAppBrowser>
<CreateRoom />
</CheckInAppBrowser>
</CheckAuth>
),
},
{
path: '/participate',
element: (
<CheckInAppBrowser>
<InputCode />
</CheckInAppBrowser>
<CheckAuth>
<CheckInAppBrowser>
<InputCode />
</CheckInAppBrowser>
</CheckAuth>
),
},
{
path: '/name',
element: <InputName />,
element: (
<CheckAuth>
<InputName />
</CheckAuth>
),
},
{
path: '/game',
Expand Down
6 changes: 6 additions & 0 deletions src/axios/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Chat,
ChatRequest,
DeadResult,
GameExist,
GameInfo,
GamesResults,
MafiaVoteResult,
Expand Down Expand Up @@ -89,6 +90,11 @@ export const startGame = async () => {
export const getRoomsResults = () => {
return http.get<GamesResults>('/games/result');
};

export const existGame = () => {
return http.get<GameExist>('/games/valid');
};

export const getChats = () => {
return http.get<Chat[]>(`/chat`);
};
Expand Down
Empty file removed src/hooks/useInAppBrowser
Empty file.
2 changes: 1 addition & 1 deletion src/pages/InputName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLInputElement>) => {
if (e.target.value.length <= 10) {
Expand All @@ -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<HTMLFormElement>) => {
event.preventDefault();

Expand Down
24 changes: 24 additions & 0 deletions src/routers/CheckAuth.tsx
Original file line number Diff line number Diff line change
@@ -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}</>;
}
4 changes: 4 additions & 0 deletions src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ export interface GamesResults {
winner: Player[];
loser: Player[];
}

export interface GameExist {
valid: boolean;
}
Loading