Files
peakskills/app/api/auth/route.ts
Julien Froidefond 42217c1c13 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.
2025-08-25 16:33:10 +02:00

113 lines
3.0 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { AuthService, userService, TeamsService } from "@/services";
export async function GET(request: NextRequest) {
try {
// Récupérer l'UUID utilisateur depuis le cookie
const userUuid = await AuthService.getUserUuidFromCookie();
if (!userUuid) {
return NextResponse.json({ error: "Non authentifié" }, { status: 401 });
}
// Récupérer le profil utilisateur
const userProfile = await userService.getUserByUuid(userUuid);
if (!userProfile) {
return NextResponse.json(
{ error: "Utilisateur non trouvé" },
{ status: 404 }
);
}
// 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("Auth GET error:", error);
return NextResponse.json(
{ error: "Erreur interne du serveur" },
{ status: 500 }
);
}
}
/**
* POST /api/auth - Authentifie un utilisateur et créé/met à jour le cookie
*/
export async function POST(request: NextRequest) {
try {
const profile: UserProfile = await request.json();
if (!profile.firstName || !profile.lastName || !profile.teamId) {
return NextResponse.json(
{ error: "Missing required fields" },
{ status: 400 }
);
}
// Authentifier l'utilisateur et récupérer la configuration du cookie
const { userUuid, cookieConfig } = await AuthService.authenticateUser(
profile
);
// Créer la réponse avec le cookie
const response = NextResponse.json(
{
user: { ...profile, uuid: userUuid },
userUuid,
},
{ status: 200 }
);
// Définir le cookie avec l'UUID utilisateur
response.cookies.set(
cookieConfig.name,
cookieConfig.value,
cookieConfig.options
);
return response;
} catch (error) {
console.error("Error authenticating user:", error);
return NextResponse.json(
{ error: "Failed to authenticate user" },
{ status: 500 }
);
}
}
/**
* DELETE /api/auth - Déconnecte l'utilisateur (supprime le cookie)
*/
export async function DELETE() {
try {
const response = NextResponse.json({ success: true }, { status: 200 });
response.cookies.set(COOKIE_NAME, "", { maxAge: 0 });
return response;
} catch (error) {
console.error("Error logging out user:", error);
return NextResponse.json({ error: "Failed to logout" }, { status: 500 });
}
}