export async function joinGameAction()

in app/actions/join-game.ts [24:44]


export async function joinGameAction({gameId, tokens}: {gameId: string, tokens: Tokens}) {
  const authUser = await validateTokens(tokens);

  // Parse request (throw an error if not correct)
  GameIdSchema.parse(gameId);

  const gameRef = await gamesRef.doc(gameId);
  const gameDoc = await gameRef.get();
  const game = GameSchema.parse(gameDoc.data());
  const playerIdList = Object.keys(game.players);
  if (playerIdList.includes(authUser.uid)) return;
  if (game.leader.uid === authUser.uid) {
    // Respond with JSON indicating no game was found
    throw new Error('The game leader may not be a player.');
  }

  // update database to join the game
  await gameRef.update({
    [`players.${authUser.uid}`]: generateName(authUser.uid),
  });
}