diff --git a/app/api/evaluations/skills/route.ts b/app/api/evaluations/skills/route.ts index 63e653c..54f9199 100644 --- a/app/api/evaluations/skills/route.ts +++ b/app/api/evaluations/skills/route.ts @@ -1,30 +1,11 @@ import { NextRequest, NextResponse } from "next/server"; -import { cookies } from "next/headers"; +import { AuthService } from "@/services/auth-service"; import { evaluationService } from "@/services/evaluation-service"; -import { userService } from "@/services/user-service"; - -const COOKIE_NAME = "peakSkills_userId"; export async function PUT(request: NextRequest) { try { // Récupérer l'utilisateur depuis le cookie (maintenant un UUID) - const cookieStore = await cookies(); - const userUuid = cookieStore.get(COOKIE_NAME)?.value; - - if (!userUuid) { - return NextResponse.json( - { error: "Utilisateur non authentifié" }, - { status: 401 } - ); - } - - const userProfile = await userService.getUserByUuid(userUuid); - if (!userProfile) { - return NextResponse.json( - { error: "Utilisateur introuvable" }, - { status: 404 } - ); - } + const { userProfile } = await AuthService.requireAuthenticatedUser(); const body = await request.json(); const { category, skillId, level, canMentor, wantsToLearn, action } = body; diff --git a/services/auth-service.ts b/services/auth-service.ts index 40cdb60..aa64d0c 100644 --- a/services/auth-service.ts +++ b/services/auth-service.ts @@ -33,6 +33,32 @@ export class AuthService { return !!userUuid; } + /** + * Vérifie l'authentification et retourne le profil utilisateur + * @throws {Error} avec status 401 si non authentifié ou 404 si utilisateur non trouvé + */ + static async requireAuthenticatedUser(): Promise<{ + userUuid: string; + userProfile: UserProfile; + }> { + const userUuid = await this.getUserUuidFromCookie(); + + if (!userUuid) { + const error = new Error("Utilisateur non authentifié"); + (error as any).status = 401; + throw error; + } + + const userProfile = await userService.getUserByUuid(userUuid); + if (!userProfile) { + const error = new Error("Utilisateur introuvable"); + (error as any).status = 404; + throw error; + } + + return { userUuid, userProfile }; + } + /** * Authentifie un utilisateur et retourne la configuration du cookie */