Files
peakskills/app/api/evaluations/route.ts
Julien Froidefond 45fb1148ae feat: enhance evaluation loading with cookie authentication
- Updated the GET method in the evaluations route to support user authentication via cookies, improving security and user experience.
- Added compatibility for legacy parameter-based authentication to ensure backward compatibility.
- Refactored the useEvaluation hook to load user profiles from cookies instead of localStorage, streamlining the authentication process.
- Introduced a new method in EvaluationService to retrieve user profiles by ID, enhancing data retrieval efficiency.
- Updated ApiClient to handle cookie-based requests for loading evaluations, ensuring proper session management.
2025-08-21 11:55:50 +02:00

75 lines
2.3 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { cookies } from "next/headers";
import { evaluationService } from "@/services/evaluation-service";
import { UserEvaluation, UserProfile } from "@/lib/types";
import { COOKIE_NAME } from "@/lib/auth-utils";
export async function GET(request: NextRequest) {
try {
const cookieStore = await cookies();
const userId = cookieStore.get(COOKIE_NAME)?.value;
const userIdNum = userId ? parseInt(userId) : null;
// Support pour l'ancien mode avec paramètres (pour la compatibilité)
if (!userIdNum) {
const { searchParams } = new URL(request.url);
const firstName = searchParams.get("firstName");
const lastName = searchParams.get("lastName");
const teamId = searchParams.get("teamId");
if (!firstName || !lastName || !teamId) {
return NextResponse.json(
{ error: "Utilisateur non authentifié" },
{ status: 401 }
);
}
const profile: UserProfile = { firstName, lastName, teamId };
const evaluation = await evaluationService.loadUserEvaluation(profile);
return NextResponse.json({ evaluation });
}
// Mode authentifié par cookie
const userProfile = await evaluationService.getUserById(userIdNum);
if (!userProfile) {
return NextResponse.json(
{ error: "Utilisateur non trouvé" },
{ status: 404 }
);
}
const evaluation = await evaluationService.loadUserEvaluation(userProfile);
return NextResponse.json({ evaluation });
} catch (error) {
console.error("Erreur lors du chargement de l'évaluation:", error);
return NextResponse.json(
{ error: "Erreur interne du serveur" },
{ status: 500 }
);
}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const evaluation: UserEvaluation = body.evaluation;
if (!evaluation || !evaluation.profile) {
return NextResponse.json(
{ error: "Évaluation invalide" },
{ status: 400 }
);
}
await evaluationService.saveUserEvaluation(evaluation);
return NextResponse.json({ success: true });
} catch (error) {
console.error("Erreur lors de la sauvegarde de l'évaluation:", error);
return NextResponse.json(
{ error: "Erreur interne du serveur" },
{ status: 500 }
);
}
}