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:
Julien Froidefond
2025-08-21 11:55:50 +02:00
parent 5cb2bad992
commit 45fb1148ae
6 changed files with 270 additions and 32 deletions

71
lib/auth-utils.ts Normal file
View File

@@ -0,0 +1,71 @@
"use client";
import { UserProfile } from "./types";
/**
* Service d'authentification côté client
*/
export class AuthService {
/**
* Authentifie un utilisateur et créé le cookie
*/
static async login(
profile: UserProfile
): Promise<{ user: UserProfile & { id: number }; userId: number }> {
const response = await fetch("/api/auth", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(profile),
});
if (!response.ok) {
throw new Error("Failed to authenticate user");
}
return response.json();
}
/**
* Récupère l'utilisateur actuel depuis le cookie
*/
static async getCurrentUser(): Promise<UserProfile | null> {
try {
const response = await fetch("/api/auth", {
method: "GET",
credentials: "same-origin",
});
if (!response.ok) {
return null;
}
const data = await response.json();
return data.user;
} catch (error) {
console.error("Failed to get current user:", error);
return null;
}
}
/**
* Déconnecte l'utilisateur (supprime le cookie)
*/
static async logout(): Promise<void> {
const response = await fetch("/api/auth", {
method: "DELETE",
credentials: "same-origin",
});
if (!response.ok) {
throw new Error("Failed to logout");
}
}
}
/**
* Constantes pour les cookies
*/
export const COOKIE_NAME = "peakSkills_userId";
export const COOKIE_MAX_AGE = 30 * 24 * 60 * 60; // 30 jours