mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
import { query, queryOne } from "@/lib/db";
|
|
import { getToken } from "next-auth/jwt";
|
|
|
|
export async function GET(request) {
|
|
try {
|
|
const token = await getToken({
|
|
req: request,
|
|
secret:
|
|
process.env.NEXTAUTH_SECRET ||
|
|
"your-fallback-secret-should-be-at-least-32-chars",
|
|
});
|
|
if (!token) {
|
|
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
if (!token.teamId) {
|
|
return Response.json(
|
|
{ error: "You are not part of a team" },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
const team = await queryOne("SELECT * FROM teams WHERE id = ?", [
|
|
token.teamId,
|
|
]);
|
|
if (!team) {
|
|
return Response.json({ error: "Team not found" }, { status: 404 });
|
|
}
|
|
// Optionally, fetch additional team details (e.g. team members)
|
|
const members = await query(
|
|
"SELECT id, username FROM users WHERE team_id = ? ORDER BY username ASC",
|
|
[token.teamId]
|
|
);
|
|
return Response.json(
|
|
{ success: true, team, members },
|
|
{
|
|
status: 200,
|
|
headers: {
|
|
"Cache-Control": "no-store, must-revalidate, max-age=0",
|
|
Pragma: "no-cache",
|
|
Expires: "0",
|
|
},
|
|
}
|
|
);
|
|
} catch (error) {
|
|
console.error("Team fetch error:", error);
|
|
return Response.json(
|
|
{ error: "Failed to fetch team data" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|