mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
fixed all issues regarding team's member list page where users couldn't leave properly or where they couldn't join properly. the team selection page will now directly communicate with the database to ensure everything stays up to date. The team member list page will also auto update every few seconds so the user will not have to manually update their page.
This commit is contained in:
1 parent
10c117e223
commit
673ea41a4c
11 files changed
+373
-36
No files matched your search
@@ -0,0 +1,89 @@
|
||||
import { run } from "@/lib/db";
|
||||
import { cookies } from "next/headers";
|
||||
import { verifyToken, createToken } from "@/app/api/auth/[...nextauth]/route";
|
||||
|
||||
export async function POST(request, context) {
|
||||
try {
|
||||
// Verify user authentication
|
||||
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 });
|
||||
}
|
||||
|
||||
// Get teamId from URL params
|
||||
const params = await Promise.resolve(context.params);
|
||||
const teamId = params?.teamId;
|
||||
|
||||
// Validate teamId
|
||||
if (!teamId || isNaN(parseInt(teamId, 10))) {
|
||||
return Response.json({ error: "Invalid team ID" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Check if user is actually in this team
|
||||
if (userData.teamId != teamId) {
|
||||
return Response.json(
|
||||
{ error: "You are not part of this team" },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
// Update the user record to remove team association
|
||||
await run("UPDATE users SET team_id = NULL WHERE id = ?", [userData.id]);
|
||||
|
||||
// Try to update the team's last_updated timestamp but don't fail if column doesn't exist
|
||||
try {
|
||||
await run("UPDATE teams SET last_updated = ? WHERE id = ?", [
|
||||
new Date().toISOString(),
|
||||
teamId,
|
||||
]);
|
||||
} catch (updateError) {
|
||||
// Log but don't fail if we can't update timestamp
|
||||
console.warn(
|
||||
"Failed to update last_updated timestamp:",
|
||||
updateError.message
|
||||
);
|
||||
}
|
||||
|
||||
// Create a new token with updated user data (without teamId)
|
||||
const newUserData = {
|
||||
...userData,
|
||||
teamId: null, // Remove team association in token
|
||||
iat: Math.floor(Date.now() / 1000),
|
||||
exp: Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60, // 30 days
|
||||
};
|
||||
|
||||
const newToken = createToken(newUserData);
|
||||
|
||||
// Calculate expiration date for cookie
|
||||
const expiryDate = new Date();
|
||||
expiryDate.setDate(expiryDate.getDate() + 30);
|
||||
|
||||
// Return success with updated session token
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
message: "Successfully left the team",
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Set-Cookie": `next-auth.session-token=${newToken}; Path=/; HttpOnly; SameSite=Lax; Expires=${expiryDate.toUTCString()}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Leave team error:", error);
|
||||
return Response.json(
|
||||
{ error: "Failed to leave team: " + error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -43,17 +43,21 @@ export async function GET(request, context) {
|
||||
|
||||
const timestamp = new Date().toISOString(); // Add timestamp for debugging
|
||||
|
||||
// Add additional cache-busting headers
|
||||
return Response.json(
|
||||
{
|
||||
success: true,
|
||||
team,
|
||||
members,
|
||||
refreshed: timestamp,
|
||||
lastUpdated: team.last_updated || timestamp,
|
||||
},
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
"Cache-Control": "no-store, max-age=0", // Prevent caching
|
||||
"Cache-Control": "no-store, must-revalidate, max-age=0",
|
||||
Pragma: "no-cache",
|
||||
Expires: "0",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import { query, run } from "@/lib/db";
|
||||
import { cookies } from "next/headers";
|
||||
import { verifyToken } from "@/app/api/auth/[...nextauth]/route";
|
||||
|
||||
export async function POST(request, context) {
|
||||
try {
|
||||
// Verify user authentication
|
||||
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 });
|
||||
}
|
||||
|
||||
// Get teamId from URL params
|
||||
const params = await Promise.resolve(context.params);
|
||||
const teamId = params?.teamId;
|
||||
|
||||
// Validate teamId
|
||||
if (!teamId || isNaN(parseInt(teamId, 10))) {
|
||||
return Response.json({ error: "Invalid team ID" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Try to update the last_updated timestamp for this team
|
||||
try {
|
||||
await run("UPDATE teams SET last_updated = ? WHERE id = ?", [
|
||||
new Date().toISOString(),
|
||||
teamId,
|
||||
]);
|
||||
} catch (updateError) {
|
||||
// Log but don't fail if we can't update timestamp
|
||||
console.warn(
|
||||
"Failed to update last_updated timestamp:",
|
||||
updateError.message
|
||||
);
|
||||
|
||||
// Try to alter the table to add the column
|
||||
try {
|
||||
await run(
|
||||
"ALTER TABLE teams ADD COLUMN last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
|
||||
);
|
||||
console.log("Added missing last_updated column to teams table");
|
||||
|
||||
// Try the update again
|
||||
await run("UPDATE teams SET last_updated = ? WHERE id = ?", [
|
||||
new Date().toISOString(),
|
||||
teamId,
|
||||
]);
|
||||
} catch (alterError) {
|
||||
console.error("Failed to alter teams table:", alterError.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Return success response
|
||||
return Response.json({
|
||||
success: true,
|
||||
message: "Successfully updated team members list",
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Team members update error:", error);
|
||||
return Response.json(
|
||||
{ error: "Failed to update team members: " + error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user