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:
71
lib/auth-utils.ts
Normal file
71
lib/auth-utils.ts
Normal 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
|
||||
Reference in New Issue
Block a user