Files
peworkshop/app/api/user/route.js
T
2025-03-14 23:50:25 +00:00

61 lines
1.7 KiB
JavaScript

// 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 {
const cookieStore = await cookies();
const sessionToken =
cookieStore.get("next-auth.session-token")?.value ||
cookieStore.get("__Secure-next-auth.session-token")?.value;
if (!sessionToken) {
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 });
}
} catch (error) {
console.error("User API error:", error);
return Response.json({ error: "Server error" }, { status: 500 });
}
}