Skip to content

Commit

Permalink
Only allow game creator to view manage_game page
Browse files Browse the repository at this point in the history
  • Loading branch information
staadecker committed Mar 12, 2024
1 parent ecaf16f commit 974121b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 20 deletions.
11 changes: 9 additions & 2 deletions src/firebase/FirebaseContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { FirebaseApp, initializeApp } from "firebase/app";
import { Auth, User, getAuth, signInAnonymously } from "firebase/auth";
import {
Auth,
User,
browserLocalPersistence,
getAuth,
initializeAuth,
signInAnonymously,
} from "firebase/auth";
import { Firestore, getFirestore } from "firebase/firestore";
import { createContext, useContext, useEffect, useState } from "react";
import Loading from "../components/Loading";
Expand All @@ -25,7 +32,7 @@ export const FirebaseProvider = (props: { children: React.ReactNode }) => {
messagingSenderId: "774018965451",
appId: "1:774018965451:web:19c826207d24e9d0b8204e",
});
const auth = getAuth(app);
const auth = initializeAuth(app, { persistence: browserLocalPersistence });
const db = getFirestore(app);

useEffect(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/firebase/db_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export const loadPriorWorks = async (

export const createGame = async (
db: Firestore,
uid: string,
waitTime: number,
roundDuration: number,
numRounds: number,
Expand All @@ -134,6 +135,7 @@ export const createGame = async (
roundDuration,
numRounds,
question,
gameManager: uid,
});
return gameId;
};
Expand Down
1 change: 1 addition & 0 deletions src/firebase/db_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type GameMetaData = {
numRounds: number;
roundDuration: number;
question: string;
gameManager: string;
};

export type GameUsers = {
Expand Down
12 changes: 10 additions & 2 deletions src/game_logic/state_machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export type PossibleState =
| "loading_work"
| "in_progress"
| "saving_work"
| "finished";
| "finished"
| "permission_denied";

export const useGameStateMachine = (gameId: string, isPlayer: boolean) => {
const { db, currentUser } = useFirebase();
Expand All @@ -35,10 +36,16 @@ export const useGameStateMachine = (gameId: string, isPlayer: boolean) => {
{ on?: Record<any, PossibleState>; effect?: Function }
> = {
loading_game: {
on: { GAME_FOUND: "loaded", game_not_found: "game_not_found" },
on: {
GAME_FOUND: "loaded",
game_not_found: "game_not_found",
permission_denied: "permission_denied",
},
effect({ send, context, setContext }: EffectArgs) {
getGameMetadata(context, (meta) => {
if (meta === null) send("game_not_found");
else if (!isPlayer && meta.gameManager !== currentUser.uid)
send("permission_denied");
else
setContext((context) => ({ ...context, meta })).send("GAME_FOUND");
});
Expand All @@ -52,6 +59,7 @@ export const useGameStateMachine = (gameId: string, isPlayer: boolean) => {
});
},
},
permission_denied: {},
game_not_found: {},
loaded: {
on: {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/CreateGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createGame } from "../firebase/db_queries";
import { Card } from "@mui/material";

const CreateGame = () => {
const { db } = useFirebase();
const { db, currentUser } = useFirebase();
const navigate = useNavigate();
const [editor] = useLexicalComposerContext();
const [submitting, setSubmitting] = useState(false);
Expand All @@ -27,6 +27,7 @@ const CreateGame = () => {

const gameId = await createGame(
db,
currentUser.uid,
Number(formData.get("waitingTime")),
Number(formData.get("roundDuration")),
Number(formData.get("numRounds")),
Expand Down
33 changes: 18 additions & 15 deletions src/routes/ManageGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ const ManageGameRouter = () => {
case "not_enough_players":
return <MessagePage msg="Not enough players :/" />;
case "waiting":
return <ManageGamePage />;
case "in_progress":
case "saving_work":
case "already_started":
case "loading_work":
return <MessagePage msg="Game in progress!" />;
return <ManageGamePage />;
case "finished":
return <MessagePage msg="Game finished!" />;
case "permission_denied":
return <MessagePage msg="You can only manage games you've created!" error />;
default:
return (
<MessagePage
Expand All @@ -55,20 +56,22 @@ const ManageGamePage = () => {
return (
<div className="flex flex-col">
<GameToolbar />
<div className="flex flex-row h-full w-full p-8 items-stretch">
<div className="flex flex-col items-center basis-1/2">
<h1 className="text-4xl py-4">Join Game</h1>
<div className="rounded-xl bg-white text-center space-y-0 pb-8 px-4">
<QRCode />
<a
href={link}
className="underline text-4xl font-bold text-blue-900 py-8"
target="_blank"
>
{link.split("//")[1]}
</a>
<div className="flex flex-row h-full w-full p-8 items-stretch justify-center">
{state.value === "waiting" && (
<div className="flex flex-col items-center basis-1/2">
<h1 className="text-4xl py-4">Join Game</h1>
<div className="rounded-xl bg-white text-center space-y-0 pb-8 px-4">
<QRCode />
<a
href={link}
className="underline text-4xl font-bold text-blue-900 py-8"
target="_blank"
>
{link.split("//")[1]}
</a>
</div>
</div>
</div>
)}
<div className="basis-1/2 flex flex-col">
<UserList />
</div>
Expand Down

0 comments on commit 974121b

Please sign in to comment.