From 95b4f9cc07aff4e46bb3926895699dbb0d4d5644 Mon Sep 17 00:00:00 2001 From: mudabbir-ahmad Date: Wed, 12 Mar 2025 20:20:30 +0000 Subject: [PATCH] Made it so the team members page shows more than one user and fixed the issue so that multiple users can join the same team. Team GUI needs to be worked on however. --- app/(auth)/login/page.js | 31 --------------- app/(auth)/register/page.js | 31 --------------- app/(main)/team-selection/page.js | 1 + app/api/join-team/route.js | 61 ++++++++++++++++-------------- app/api/teams/[teamId]/route.js | 19 ++++++++-- clue_hunt.db | Bin 40960 -> 40960 bytes 6 files changed, 50 insertions(+), 93 deletions(-) diff --git a/app/(auth)/login/page.js b/app/(auth)/login/page.js index 8dfb23a..2af7fda 100644 --- a/app/(auth)/login/page.js +++ b/app/(auth)/login/page.js @@ -129,21 +129,6 @@ export default function Login() { -
-
-
🏠
-
-
-
📊
-
-
-
🏆
-
-
-
👤
-
-
- ); diff --git a/app/(auth)/register/page.js b/app/(auth)/register/page.js index f747cfb..cbf9ffe 100644 --- a/app/(auth)/register/page.js +++ b/app/(auth)/register/page.js @@ -105,21 +105,6 @@ export default function Register() { -
-
-
🏠
-
-
-
📊
-
-
-
🏆
-
-
-
👤
-
-
- ); diff --git a/app/(main)/team-selection/page.js b/app/(main)/team-selection/page.js index 2ae3eab..7937c3d 100644 --- a/app/(main)/team-selection/page.js +++ b/app/(main)/team-selection/page.js @@ -41,6 +41,7 @@ export default function TeamSelection() { throw new Error(data.error || "Failed to join team"); } + // Navigate to the team page using the correct URL format window.location.href = `/${data.teamId}`; } catch (err) { setError(err.message); diff --git a/app/api/join-team/route.js b/app/api/join-team/route.js index d414251..9ab65d2 100644 --- a/app/api/join-team/route.js +++ b/app/api/join-team/route.js @@ -1,46 +1,51 @@ -import { queryOne } from "../../../../lib/db"; -import { getServerSession } from "next-auth/next"; -import { authOptions } from "../../auth/[...nextauth]/route"; +import { queryOne, run } from "@/lib/db"; +import { cookies } from "next/headers"; +import { verifyToken } from "@/app/api/auth/[...nextauth]/route"; export async function POST(request) { - const session = await getServerSession(authOptions); - - if (!session) { - return new Response(JSON.stringify({ error: "Unauthorized" }), { - status: 401, - }); - } - try { - const { teamCode } = await request.json(); - if (!teamCode) { - return new Response(JSON.stringify({ error: "Team code is required" }), { - status: 400, - }); + // Use our custom token verification instead of getServerSession + const cookieStore = await cookies(); + const sessionToken = cookieStore.get("next-auth.session-token")?.value; + + if (!sessionToken) { + return Response.json({ error: "Unauthorized" }, { status: 401 }); } - const team = await queryOne("SELECT id FROM teams WHERE code = ?", [ + const userData = verifyToken(sessionToken); + if (!userData) { + return Response.json({ error: "Invalid session" }, { status: 401 }); + } + + const { teamCode } = await request.json(); + if (!teamCode) { + return Response.json({ error: "Team code is required" }, { status: 400 }); + } + + const team = await queryOne("SELECT id, name FROM teams WHERE code = ?", [ teamCode, ]); if (!team) { - return new Response(JSON.stringify({ error: "Invalid team code" }), { - status: 404, - }); + return Response.json({ error: "Invalid team code" }, { status: 404 }); } - await queryOne("UPDATE users SET team_id = ? WHERE id = ?", [ + // Update the user's team ID + await run("UPDATE users SET team_id = ? WHERE id = ?", [ team.id, - session.user.id, + userData.id, ]); - return new Response(JSON.stringify({ teamId: team.id }), { - status: 200, - }); + return Response.json( + { + teamId: team.id, + teamName: team.name, + success: true, + }, + { status: 200 } + ); } catch (error) { console.error("Join team error:", error); - return new Response(JSON.stringify({ error: "Failed to join team" }), { - status: 500, - }); + return Response.json({ error: "Failed to join team" }, { status: 500 }); } } diff --git a/app/api/teams/[teamId]/route.js b/app/api/teams/[teamId]/route.js index 4986a09..8656cf7 100644 --- a/app/api/teams/[teamId]/route.js +++ b/app/api/teams/[teamId]/route.js @@ -2,6 +2,8 @@ import { query, queryOne } from "@/lib/db"; import { cookies } from "next/headers"; import { verifyToken } from "@/app/api/auth/[...nextauth]/route"; +export const dynamic = "force-dynamic"; // Disable static rendering and caching + export async function GET(request, context) { try { // Use our custom token verification @@ -17,7 +19,9 @@ export async function GET(request, context) { return Response.json({ error: "Invalid session" }, { status: 401 }); } - const teamId = context.params.teamId; + // Properly await the params object before accessing it + const params = context.params || {}; + const teamId = params.teamId; // Validate teamId if (!teamId || isNaN(parseInt(teamId, 10))) { @@ -30,18 +34,27 @@ export async function GET(request, context) { return Response.json({ error: "Team not found" }, { status: 404 }); } + // Use a fresh query to get the latest team members each time const members = await query( - "SELECT id, username FROM users WHERE team_id = ?", + "SELECT id, username FROM users WHERE team_id = ? ORDER BY username ASC", [teamId] ); + const timestamp = new Date().toISOString(); // Add timestamp for debugging + return Response.json( { success: true, team, members, + refreshed: timestamp, }, - { status: 200 } + { + status: 200, + headers: { + "Cache-Control": "no-store, max-age=0", // Prevent caching + }, + } ); } catch (error) { console.error("Team fetch error:", error); diff --git a/clue_hunt.db b/clue_hunt.db index 92a3476ac900886dacb66f53785b5b2ffd135225..3c1330c938008bc5d88f290b25c48544763cf962 100644 GIT binary patch delta 159 zcmZoTz|?SnX@WE(=R_H2M$U~1rS^=>ldsw9aWV6YG4OBXPvIBatSG?2@8`qJ&LG;z zD$1CYpJb$Bl%!&4ppsQqQRd@hnqpG!=Vwq{;8u}p;OgzKU#S-nrEeM<;%MmSXPjp0 z>X#dy6B+88XX+Yi5aC{0oLa;>ImLeg(28da{NMTC^FP}x7;uVz@{M>tP7Y>>4a}S0 I#|t_D0Fw4DeEpkJFNwn^~IC$u(s2`*=YI0M;lNr2qf`