diff --git a/app/(main)/team/[teamId]/page.js b/app/(main)/team/[teamId]/page.js index 50fa4b0..a313431 100644 --- a/app/(main)/team/[teamId]/page.js +++ b/app/(main)/team/[teamId]/page.js @@ -25,7 +25,7 @@ export default function TeamPage({ params }) { headers: { "Content-Type": "application/json", }, - credentials: "include", // Important for auth + credentials: "include", }); if (!response.ok) { @@ -74,78 +74,155 @@ export default function TeamPage({ params }) { } return ( -
-
-
-
Team Name: {team.name}
-
Team Join Code: {team.code}
+
+
+ + ← + +
{team.name}
+
{team.code}
+
00:00
+
+ +
+
Add Users
+ +
+ {teamMembers.map((member, index) => ( +
+ User #{index + 1} +
+ ))} + +
+
+
+
-
- {teamMembers.map((member) => ( -
- {member.username} -
- ))} +
+
+
🏠
+
+
+
📊
+
+
+
🏆
+
+
+
👤
+
diff --git a/app/(main)/team/[teamId]/route.js b/app/(main)/team/[teamId]/route.js index bc57b7c..956fae0 100644 --- a/app/(main)/team/[teamId]/route.js +++ b/app/(main)/team/[teamId]/route.js @@ -1,105 +1,30 @@ -// app/api/teams/[teamId]/route.js +// app/(main)/team/[teamId]/route.js import { queryOne, query } from "../../../../lib/db"; import { getServerSession } from "next-auth/next"; import { authOptions } from "../../../api/auth/[...nextauth]/route"; +import { redirect } from "next/navigation"; export async function GET(request, context) { const session = await getServerSession(authOptions); if (!session) { - return new Response(JSON.stringify({ error: "Unauthorised" }), { - status: 401, - }); + // Redirect to login page instead of returning JSON + return redirect("/login"); } try { - const { teamId } = await context.params; // Await the params + const { teamId } = context.params; - // Convert teamId to number for proper comparison - const requestedTeamId = parseInt(teamId, 10); - const userTeamId = parseInt(session.user.teamId, 10); + // Instead of returning JSON, we should let the page component handle rendering + // This route.js file should only be used for API endpoints, not page rendering - // Log for debugging - console.log("Requested team:", requestedTeamId); - console.log("User team:", userTeamId); - console.log("User role:", session.user.role); - - // Ensure the user is requesting their own team or is an admin - if (userTeamId !== requestedTeamId && session.user.role !== "admin") { - return new Response( - JSON.stringify({ error: "Not authorized to view this team" }), - { status: 403 } - ); - } - - const team = await queryOne( - "SELECT id, name, code FROM teams WHERE id = ?", - [requestedTeamId] - ); - - if (!team) { - return new Response(JSON.stringify({ error: "Team not found" }), { - status: 404, - }); - } - - const members = await query( - "SELECT id, username FROM users WHERE team_id = ?", - [requestedTeamId] - ); - - return new Response(JSON.stringify({ team, members }), { status: 200 }); + // Redirect to the page component + return redirect(`/team/${teamId}`); } catch (error) { + console.error("Team fetch error:", error); return new Response( JSON.stringify({ error: "Failed to fetch team details" }), { status: 500 } ); } } - -export async function PATCH(request, context) { - const session = await getServerSession(authOptions); - - if (!session) { - return new Response(JSON.stringify({ error: "Unauthorised" }), { - status: 401, - }); - } - - try { - const { teamId } = await context.params; // Await the params - const requestedTeamId = parseInt(teamId, 10); - const { userId } = await request.json(); // Parse the request body - - // Log for debugging - console.log("Requested team:", requestedTeamId); - console.log("User role:", session.user.role); - - // Ensure the user is an admin or the user themselves - if (session.user.role !== "admin" && session.user.id !== userId) { - return new Response( - JSON.stringify({ error: "Not authorized to change this team" }), - { status: 403 } - ); - } - - // Update the user's team - await queryOne("UPDATE users SET teamId = ? WHERE id = ?", [ - requestedTeamId, - userId, - ]); - - // Update the session with the new teamId - session.user.teamId = requestedTeamId; - - return new Response( - JSON.stringify({ success: "Team updated successfully" }), - { status: 200 } - ); - } catch (error) { - console.error("Team update error:", error); - return new Response(JSON.stringify({ error: "Failed to update team" }), { - status: 500, - }); - } -} diff --git a/app/api/teams/[teamId]/route.js b/app/api/teams/[teamId]/route.js index d317871..e26b6a5 100644 --- a/app/api/teams/[teamId]/route.js +++ b/app/api/teams/[teamId]/route.js @@ -1,3 +1,4 @@ +// app/api/teams/[teamId]/route.js import { queryOne, query } from "../../../../lib/db"; import { getServerSession } from "next-auth/next"; import { authOptions } from "../../auth/[...nextauth]/route"; @@ -35,7 +36,12 @@ export async function GET(request, { params }) { [teamId] ); - return new Response(JSON.stringify({ team, members }), { status: 200 }); + return new Response(JSON.stringify({ team, members }), { + status: 200, + headers: { + "Content-Type": "application/json", + }, + }); } catch (error) { console.error("Team fetch error:", error); return new Response(