mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
39 lines
1.2 KiB
JavaScript
39 lines
1.2 KiB
JavaScript
// app/api/user/route.js
|
|
import { cookies } from "next/headers";
|
|
|
|
export async function GET() {
|
|
try {
|
|
// Get the token from cookies - Fix: await the cookies() call
|
|
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 - using a direct import
|
|
const { verifyToken } = await import("../auth/[...nextauth]/route");
|
|
|
|
try {
|
|
const userData = verifyToken(sessionToken);
|
|
|
|
if (!userData) {
|
|
return Response.json({ error: "Invalid session" }, { status: 401 });
|
|
}
|
|
|
|
return Response.json({
|
|
id: userData.id,
|
|
username: userData.username,
|
|
role: userData.role,
|
|
teamId: userData.teamId,
|
|
});
|
|
} 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 });
|
|
}
|
|
}
|