feat: enhance user authentication and profile retrieval

- Updated GET handler in auth route to fetch user UUID from cookie using AuthService.
- Improved error handling for unauthenticated and non-existent users.
- Added team name retrieval for the user profile, with fallback handling.
- Refactored AuthClient to return detailed user information including team details.
- Enhanced navigation component to use a dropdown menu for user actions, improving UI/UX.
- Implemented loading state in UserContext to manage user info fetching.
This commit is contained in:
Julien Froidefond
2025-08-25 16:33:10 +02:00
parent 49804c0fa1
commit 42217c1c13
8 changed files with 517 additions and 39 deletions

View File

@@ -0,0 +1,46 @@
import { NextRequest, NextResponse } from "next/server";
import { AuthService, userService } from "@/services";
export async function PUT(request: NextRequest) {
try {
// Vérifier si l'utilisateur est connecté
const userUuid = await AuthService.getUserUuidFromCookie();
if (!userUuid) {
return NextResponse.json({ error: "Non authentifié" }, { status: 401 });
}
// Récupérer les données de mise à jour
const { firstName, lastName, teamId } = await request.json();
// Validation des données
if (!firstName || !lastName || !teamId) {
return NextResponse.json(
{ error: "Tous les champs sont requis" },
{ status: 400 }
);
}
// Mettre à jour l'utilisateur
await userService.updateUserByUuid(userUuid, {
firstName,
lastName,
teamId,
});
return NextResponse.json({
message: "Profil mis à jour avec succès",
user: {
firstName,
lastName,
teamId,
},
});
} catch (error: any) {
console.error("Profile update error:", error);
return NextResponse.json(
{ error: error.message || "Erreur lors de la mise à jour du profil" },
{ status: 500 }
);
}
}

View File

@@ -1,35 +1,52 @@
import { NextRequest, NextResponse } from "next/server";
import { cookies } from "next/headers";
import { userService } from "@/services/user-service";
import { AuthService, COOKIE_NAME } from "@/services/auth-service";
import { UserProfile } from "@/lib/types";
import { AuthService, userService, TeamsService } from "@/services";
/**
* GET /api/auth - Récupère l'utilisateur actuel depuis le cookie
*/
export async function GET() {
export async function GET(request: NextRequest) {
try {
const cookieStore = await cookies();
const userUuid = cookieStore.get(COOKIE_NAME)?.value;
// Récupérer l'UUID utilisateur depuis le cookie
const userUuid = await AuthService.getUserUuidFromCookie();
if (!userUuid) {
return NextResponse.json({ user: null }, { status: 200 });
return NextResponse.json({ error: "Non authentifié" }, { status: 401 });
}
// Récupérer le profil utilisateur
const userProfile = await userService.getUserByUuid(userUuid);
if (!userProfile) {
// Cookie invalide, le supprimer
const response = NextResponse.json({ user: null }, { status: 200 });
response.cookies.set(COOKIE_NAME, "", { maxAge: 0 });
return response;
return NextResponse.json(
{ error: "Utilisateur non trouvé" },
{ status: 404 }
);
}
return NextResponse.json({ user: userProfile }, { status: 200 });
// Récupérer le nom de l'équipe
let teamName = "Équipe non définie";
if (userProfile.teamId) {
try {
const team = await TeamsService.getTeamById(userProfile.teamId);
if (team) {
teamName = team.name;
}
} catch (error) {
console.error("Failed to fetch team name:", error);
}
}
// Retourner les informations complètes de l'utilisateur
return NextResponse.json({
user: {
firstName: userProfile.firstName,
lastName: userProfile.lastName,
teamId: userProfile.teamId,
teamName: teamName,
uuid: userUuid,
},
});
} catch (error) {
console.error("Error getting current user:", error);
console.error("Auth GET error:", error);
return NextResponse.json(
{ error: "Failed to get current user" },
{ error: "Erreur interne du serveur" },
{ status: 500 }
);
}