refactor: auth service for logic in evaluation skill PUT
This commit is contained in:
@@ -1,30 +1,11 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { cookies } from "next/headers";
|
import { AuthService } from "@/services/auth-service";
|
||||||
import { evaluationService } from "@/services/evaluation-service";
|
import { evaluationService } from "@/services/evaluation-service";
|
||||||
import { userService } from "@/services/user-service";
|
|
||||||
|
|
||||||
const COOKIE_NAME = "peakSkills_userId";
|
|
||||||
|
|
||||||
export async function PUT(request: NextRequest) {
|
export async function PUT(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
// Récupérer l'utilisateur depuis le cookie (maintenant un UUID)
|
// Récupérer l'utilisateur depuis le cookie (maintenant un UUID)
|
||||||
const cookieStore = await cookies();
|
const { userProfile } = await AuthService.requireAuthenticatedUser();
|
||||||
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 body = await request.json();
|
const body = await request.json();
|
||||||
const { category, skillId, level, canMentor, wantsToLearn, action } = body;
|
const { category, skillId, level, canMentor, wantsToLearn, action } = body;
|
||||||
|
|||||||
@@ -33,6 +33,32 @@ export class AuthService {
|
|||||||
return !!userUuid;
|
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
|
* Authentifie un utilisateur et retourne la configuration du cookie
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user