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.

This commit is contained in:
bobbert committed 2025-03-12 20:20:30 +00:00
1 parent 6b87e0f7fb
commit 95b4f9cc07
6 files changed
+50 -93

No files matched your search

+33 -28
View File
@@ -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 });
}
}