mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
FIX OF ALL ISSUES WITH LOG IN PAGE. EVERYTHING TO THE POINT OF TESTING THE MAP WORKS SO FAR.
This commit is contained in:
1 parent
84f010cb87
commit
c79a9636a7
10 files changed
+228
-233
No files matched your search
+33
-46
@@ -1,58 +1,45 @@
|
||||
// app/api/user/route.js
|
||||
import { queryOne } from "@/lib/db";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export const dynamic = "force-dynamic"; // Disable caching
|
||||
|
||||
export async function GET() {
|
||||
export async function GET(request) {
|
||||
try {
|
||||
const cookieStore = await cookies();
|
||||
const sessionToken =
|
||||
cookieStore.get("next-auth.session-token")?.value ||
|
||||
cookieStore.get("__Secure-next-auth.session-token")?.value;
|
||||
|
||||
if (!sessionToken) {
|
||||
// Use next-auth's getToken to safely decode the session token
|
||||
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 });
|
||||
}
|
||||
|
||||
const { verifyToken } = await import("../auth/[...nextauth]/route");
|
||||
|
||||
try {
|
||||
const userData = verifyToken(sessionToken);
|
||||
|
||||
if (!userData) {
|
||||
return Response.json({ error: "Invalid session" }, { status: 401 });
|
||||
}
|
||||
|
||||
const freshUserData = await queryOne(
|
||||
"SELECT id, username, role, team_id FROM users WHERE id = ?",
|
||||
[userData.id]
|
||||
);
|
||||
|
||||
if (!freshUserData) {
|
||||
return Response.json({ error: "User not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json(
|
||||
{
|
||||
id: freshUserData.id,
|
||||
username: freshUserData.username,
|
||||
role: freshUserData.role,
|
||||
teamId: freshUserData.team_id,
|
||||
timestamp: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
"Cache-Control": "no-store, must-revalidate",
|
||||
Pragma: "no-cache",
|
||||
Expires: "0",
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (tokenError) {
|
||||
console.error("Token verification error:", tokenError);
|
||||
return Response.json({ error: "Invalid session token" }, { status: 401 });
|
||||
// Fetch fresh user data from DB using token id
|
||||
const freshUserData = await queryOne(
|
||||
"SELECT id, username, role, team_id FROM users WHERE id = ?",
|
||||
[token.id]
|
||||
);
|
||||
if (!freshUserData) {
|
||||
return Response.json({ error: "User not found" }, { status: 404 });
|
||||
}
|
||||
return Response.json(
|
||||
{
|
||||
id: freshUserData.id,
|
||||
username: freshUserData.username,
|
||||
role: freshUserData.role,
|
||||
teamId: freshUserData.team_id,
|
||||
timestamp: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
"Cache-Control": "no-store, must-revalidate",
|
||||
Pragma: "no-cache",
|
||||
Expires: "0",
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("User API error:", error);
|
||||
return Response.json({ error: "Server error" }, { status: 500 });
|
||||
|
||||
+15
-59
@@ -1,80 +1,36 @@
|
||||
import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "../../auth/[...nextauth]/route";
|
||||
import { query, queryOne } from "../../../../lib/db";
|
||||
import { query, queryOne } from "@/lib/db";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
|
||||
export async function GET(request) {
|
||||
try {
|
||||
// Get the user's session with better error handling
|
||||
let session;
|
||||
try {
|
||||
session = await getServerSession(authOptions);
|
||||
} catch (sessionError) {
|
||||
console.error("Session fetch error:", sessionError);
|
||||
return Response.json(
|
||||
{ error: "Authentication error", details: sessionError.message },
|
||||
{ status: 401 }
|
||||
);
|
||||
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 });
|
||||
}
|
||||
|
||||
// Check if user is authenticated
|
||||
if (!session || !session.user) {
|
||||
return Response.json({ error: "You must be logged in" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Check if user is part of a team
|
||||
if (!session.user.teamId) {
|
||||
if (!token.teamId) {
|
||||
return Response.json(
|
||||
{ error: "You are not part of a team" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Fetch team data
|
||||
const team = await queryOne("SELECT * FROM teams WHERE id = ?", [
|
||||
session.user.teamId,
|
||||
token.teamId,
|
||||
]);
|
||||
|
||||
if (!team) {
|
||||
return Response.json({ error: "Team not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Fetch team members
|
||||
// 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",
|
||||
[session.user.teamId]
|
||||
[token.teamId]
|
||||
);
|
||||
|
||||
// Fetch hunt status to provide start time - safely check multiple places
|
||||
let huntStartTime = null;
|
||||
|
||||
try {
|
||||
// Try hunt_status table first
|
||||
const huntStatus = await queryOne("SELECT * FROM hunt_status LIMIT 1");
|
||||
if (huntStatus) {
|
||||
huntStartTime = huntStatus.start_time;
|
||||
} else {
|
||||
// Fallback to hunts table if it exists
|
||||
const oldHuntRecord = await queryOne(
|
||||
"SELECT start_time FROM hunts ORDER BY id DESC LIMIT 1"
|
||||
);
|
||||
if (oldHuntRecord) {
|
||||
huntStartTime = oldHuntRecord.start_time;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("Error fetching hunt status (non-critical):", err.message);
|
||||
// Continue even if hunt status check fails
|
||||
}
|
||||
|
||||
// Add cache-busting headers
|
||||
return Response.json(
|
||||
{
|
||||
success: true,
|
||||
team,
|
||||
members,
|
||||
huntStartTime: huntStartTime,
|
||||
refreshed: new Date().toISOString(),
|
||||
},
|
||||
{ success: true, team, members },
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
|
||||
Reference in new issue
Block a user