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

@@ -1,6 +1,13 @@
"use client";
import { createContext, useContext, useState, ReactNode } from "react";
import {
createContext,
useContext,
useState,
useEffect,
ReactNode,
} from "react";
import { authClient } from "@/clients";
interface UserInfo {
firstName: string;
@@ -11,15 +18,41 @@ interface UserInfo {
interface UserContextType {
userInfo: UserInfo | null;
setUserInfo: (userInfo: UserInfo | null) => void;
loading: boolean;
}
const UserContext = createContext<UserContextType | undefined>(undefined);
export function UserProvider({ children }: { children: ReactNode }) {
const [userInfo, setUserInfo] = useState<UserInfo | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
const fetchUserInfo = async () => {
try {
// Récupérer les informations utilisateur depuis l'API
const user = await authClient.getCurrentUser();
if (user) {
setUserInfo({
firstName: user.firstName,
lastName: user.lastName,
teamName: user.teamName || "Équipe non définie",
});
}
} catch (error) {
console.error("Failed to fetch user info:", error);
// En cas d'erreur, on considère que l'utilisateur n'est pas connecté
setUserInfo(null);
} finally {
setLoading(false);
}
};
fetchUserInfo();
}, []);
return (
<UserContext.Provider value={{ userInfo, setUserInfo }}>
<UserContext.Provider value={{ userInfo, setUserInfo, loading }}>
{children}
</UserContext.Provider>
);