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.
This commit is contained in:
@@ -1,24 +1,44 @@
|
||||
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 { searchParams } = new URL(request.url);
|
||||
const firstName = searchParams.get("firstName");
|
||||
const lastName = searchParams.get("lastName");
|
||||
const teamId = searchParams.get("teamId");
|
||||
const cookieStore = await cookies();
|
||||
const userId = cookieStore.get(COOKIE_NAME)?.value;
|
||||
const userIdNum = userId ? parseInt(userId) : null;
|
||||
|
||||
if (!firstName || !lastName || !teamId) {
|
||||
// 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: "firstName, lastName et teamId sont requis" },
|
||||
{ status: 400 }
|
||||
{ error: "Utilisateur non trouvé" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const profile: UserProfile = { firstName, lastName, teamId };
|
||||
const evaluation = await evaluationService.loadUserEvaluation(profile);
|
||||
|
||||
const evaluation = await evaluationService.loadUserEvaluation(userProfile);
|
||||
return NextResponse.json({ evaluation });
|
||||
} catch (error) {
|
||||
console.error("Erreur lors du chargement de l'évaluation:", error);
|
||||
|
||||
Reference in New Issue
Block a user