FIX OF ALL ISSUES WITH LOG IN PAGE. EVERYTHING TO THE POINT OF TESTING THE MAP WORKS SO FAR.

This commit is contained in:
bobbert committed 2025-03-15 00:22:47 +00:00
1 parent 84f010cb87
commit c79a9636a7
10 files changed
+228 -233

No files matched your search

+33 -46
View File
@@ -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 });