mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-08 03:50:21 +00:00
52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
import { queryOne, run } from "@/lib/db";
|
|
import { cookies } from "next/headers";
|
|
import { verifyToken } from "@/app/api/auth/[...nextauth]/route";
|
|
|
|
export async function POST(request) {
|
|
try {
|
|
// 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 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 Response.json({ error: "Invalid team code" }, { status: 404 });
|
|
}
|
|
|
|
// Update the user's team ID
|
|
await run("UPDATE users SET team_id = ? WHERE id = ?", [
|
|
team.id,
|
|
userData.id,
|
|
]);
|
|
|
|
return Response.json(
|
|
{
|
|
teamId: team.id,
|
|
teamName: team.name,
|
|
success: true,
|
|
},
|
|
{ status: 200 }
|
|
);
|
|
} catch (error) {
|
|
console.error("Join team error:", error);
|
|
return Response.json({ error: "Failed to join team" }, { status: 500 });
|
|
}
|
|
}
|