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:
@@ -16,6 +16,7 @@ import {
|
||||
} from "@/lib/evaluation-utils";
|
||||
import { apiClient } from "@/services/api-client";
|
||||
import { loadSkillCategories, loadTeams } from "@/lib/data-loader";
|
||||
import { AuthService } from "@/lib/auth-utils";
|
||||
|
||||
// Fonction pour migrer une évaluation existante avec de nouvelles catégories
|
||||
function migrateEvaluation(
|
||||
@@ -71,11 +72,10 @@ export function useEvaluation() {
|
||||
setSkillCategories(categories);
|
||||
setTeams(teamsData);
|
||||
|
||||
// Try to load user profile from localStorage and then load evaluation from API
|
||||
// Try to load user profile from cookie and then load evaluation from API
|
||||
try {
|
||||
const savedProfile = localStorage.getItem("peakSkills_userProfile");
|
||||
if (savedProfile) {
|
||||
const profile: UserProfile = JSON.parse(savedProfile);
|
||||
const profile = await AuthService.getCurrentUser();
|
||||
if (profile) {
|
||||
const saved = await loadUserEvaluation(profile);
|
||||
if (saved) {
|
||||
// Migrate evaluation to include new categories if needed
|
||||
@@ -88,8 +88,6 @@ export function useEvaluation() {
|
||||
}
|
||||
} catch (profileError) {
|
||||
console.error("Failed to load user profile:", profileError);
|
||||
// Clear invalid profile data
|
||||
localStorage.removeItem("peakSkills_userProfile");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to initialize data:", error);
|
||||
@@ -133,10 +131,13 @@ export function useEvaluation() {
|
||||
try {
|
||||
const categories = await loadSkillCategories();
|
||||
setSkillCategories(categories);
|
||||
|
||||
|
||||
// Si on a une évaluation en cours, la migrer avec les nouvelles catégories
|
||||
if (userEvaluation) {
|
||||
const migratedEvaluation = migrateEvaluation(userEvaluation, categories);
|
||||
const migratedEvaluation = migrateEvaluation(
|
||||
userEvaluation,
|
||||
categories
|
||||
);
|
||||
if (migratedEvaluation !== userEvaluation) {
|
||||
setUserEvaluation(migratedEvaluation);
|
||||
await saveUserEvaluation(migratedEvaluation);
|
||||
@@ -156,8 +157,8 @@ export function useEvaluation() {
|
||||
lastUpdated: new Date().toISOString(),
|
||||
};
|
||||
|
||||
// Save profile to localStorage for auto-login
|
||||
localStorage.setItem("peakSkills_userProfile", JSON.stringify(profile));
|
||||
// Authenticate user and create cookie
|
||||
await AuthService.login(profile);
|
||||
|
||||
setUserEvaluation(newEvaluation);
|
||||
await saveUserEvaluation(newEvaluation);
|
||||
@@ -371,16 +372,21 @@ export function useEvaluation() {
|
||||
lastUpdated: new Date().toISOString(),
|
||||
};
|
||||
|
||||
// Save profile to localStorage for auto-login
|
||||
localStorage.setItem("peakSkills_userProfile", JSON.stringify(profile));
|
||||
// Authenticate user and create cookie
|
||||
await AuthService.login(profile);
|
||||
|
||||
setUserEvaluation(newEvaluation);
|
||||
await saveUserEvaluation(newEvaluation);
|
||||
};
|
||||
|
||||
const clearUserProfile = () => {
|
||||
localStorage.removeItem("peakSkills_userProfile");
|
||||
setUserEvaluation(null);
|
||||
const clearUserProfile = async () => {
|
||||
try {
|
||||
await AuthService.logout();
|
||||
setUserEvaluation(null);
|
||||
} catch (error) {
|
||||
console.error("Failed to logout:", error);
|
||||
setUserEvaluation(null);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user