// app/api/user/route.js import { queryOne } from "@/lib/db"; import { cookies } from "next/headers"; export const dynamic = "force-dynamic"; // Disable caching export async function GET() { try { // Get the token from cookies const cookieStore = await cookies(); const sessionToken = cookieStore.get("next-auth.session-token")?.value; if (!sessionToken) { return Response.json({ error: "Unauthorized" }, { status: 401 }); } // Import the token verification function const { verifyToken } = await import("../auth/[...nextauth]/route"); try { const userData = verifyToken(sessionToken); if (!userData) { return Response.json({ error: "Invalid session" }, { status: 401 }); } // Always fetch the latest user data from the database to ensure it's accurate 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(), // Add timestamp for debugging }, { 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 }); } } catch (error) { console.error("User API error:", error); return Response.json({ error: "Server error" }, { status: 500 }); } }