From 3ed7028bda41e849dac3f6a37ed51c39f43110ef Mon Sep 17 00:00:00 2001 From: ectasy663 Date: Thu, 24 Sep 2026 20:20:25 +0530 Subject: [PATCH] fix: Authentication headers and dynamic userId for CBT/DPP history page --- .../dashboard/cbt/history/[attemptId]/page.tsx | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/app/dashboard/cbt/history/[attemptId]/page.tsx b/src/app/dashboard/cbt/history/[attemptId]/page.tsx index f5c9b46..c0e5d93 100644 --- a/src/app/dashboard/cbt/history/[attemptId]/page.tsx +++ b/src/app/dashboard/cbt/history/[attemptId]/page.tsx @@ -1,7 +1,9 @@ -"use client"; +"use client"; import { useEffect, useState, use } from "react"; import Link from "next/link"; +import { fetchWithAuth } from "@/lib/apiClient"; +import { useActiveUser } from "@/hooks/useActiveUser"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; @@ -18,15 +20,15 @@ export default function AttemptHistoryPage({ params }: { params: Promise<{ attem const [test, setTest] = useState(null); const [questions, setQuestions] = useState>({}); - const userId = "b0e17378-864d-439e-8806-708dc48a891b"; + const { userId } = useActiveUser(); useEffect(() => { + if (!userId) return; + async function fetchHistoryData() { try { - const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000'; - // 1. Fetch all attempts to find this specific one - const attemptsRes = await fetch(`${apiUrl}/api/users/${userId}/attempts`); + const attemptsRes = await fetchWithAuth(`/api/users/${userId}/attempts`); const attemptsData = await attemptsRes.json(); const foundAttempt = attemptsData.find((a: any) => a.id === attemptId); @@ -36,7 +38,7 @@ export default function AttemptHistoryPage({ params }: { params: Promise<{ attem setAttempt(foundAttempt); // 2. Fetch the test details - const testRes = await fetch(`${apiUrl}/api/tests/${foundAttempt.source_id}?user_id=${userId}`); + const testRes = await fetchWithAuth(`/api/tests/${foundAttempt.source_id}?user_id=${userId}`); const testData = await testRes.json(); setTest(testData); @@ -44,7 +46,7 @@ export default function AttemptHistoryPage({ params }: { params: Promise<{ attem const qs: Record = {}; await Promise.all( foundAttempt.answers.map(async (ans: any) => { - const qRes = await fetch(`${apiUrl}/api/questions/${ans.question_id}`); + const qRes = await fetchWithAuth(`/api/questions/${ans.question_id}`); if (qRes.ok) { const qData = await qRes.json(); qs[ans.question_id] = qData;