refactor: no XHR for currentUser but backside
This commit is contained in:
@@ -1,56 +0,0 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
import { AuthService, userService, TeamsService } from "@/services";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* GET /api/auth - Récupère les informations de l'utilisateur connecté
|
|
||||||
*/
|
|
||||||
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 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,6 +7,7 @@ import { ThemeProvider } from "@/components/layout/theme-provider";
|
|||||||
import { Toaster } from "@/components/ui/sonner";
|
import { Toaster } from "@/components/ui/sonner";
|
||||||
import { UserProvider } from "@/hooks/use-user-context";
|
import { UserProvider } from "@/hooks/use-user-context";
|
||||||
import { NavigationWrapper } from "@/components/layout/navigation-wrapper";
|
import { NavigationWrapper } from "@/components/layout/navigation-wrapper";
|
||||||
|
import { AuthService, TeamsService } from "@/services";
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
title: "PeakSkills - Auto-évaluation de compétences",
|
title: "PeakSkills - Auto-évaluation de compétences",
|
||||||
@@ -14,11 +15,42 @@ export const metadata: Metadata = {
|
|||||||
"Plateforme d'auto-évaluation de compétences techniques pour les équipes",
|
"Plateforme d'auto-évaluation de compétences techniques pour les équipes",
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function RootLayout({
|
export default async function RootLayout({
|
||||||
children,
|
children,
|
||||||
}: {
|
}: {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}) {
|
}) {
|
||||||
|
// Récupérer les infos utilisateur côté serveur
|
||||||
|
let userInfo = null;
|
||||||
|
try {
|
||||||
|
const { userUuid, userProfile } =
|
||||||
|
await AuthService.requireAuthenticatedUser();
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
userInfo = {
|
||||||
|
firstName: userProfile.firstName,
|
||||||
|
lastName: userProfile.lastName,
|
||||||
|
teamName,
|
||||||
|
teamId: userProfile.teamId,
|
||||||
|
uuid: userUuid,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
// Utilisateur non authentifié, userInfo reste null
|
||||||
|
console.log("User not authenticated:", error);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<html lang="en" suppressHydrationWarning>
|
<html lang="en" suppressHydrationWarning>
|
||||||
<body
|
<body
|
||||||
@@ -30,7 +62,7 @@ export default function RootLayout({
|
|||||||
enableSystem
|
enableSystem
|
||||||
disableTransitionOnChange
|
disableTransitionOnChange
|
||||||
>
|
>
|
||||||
<UserProvider>
|
<UserProvider initialUserInfo={userInfo}>
|
||||||
<NavigationWrapper />
|
<NavigationWrapper />
|
||||||
<main className="min-h-screen">{children}</main>
|
<main className="min-h-screen">{children}</main>
|
||||||
<Toaster />
|
<Toaster />
|
||||||
|
|||||||
@@ -47,31 +47,4 @@ export class AuthClient extends BaseHttpClient {
|
|||||||
async logout(): Promise<{ message: string }> {
|
async logout(): Promise<{ message: string }> {
|
||||||
return await this.post("/auth/logout");
|
return await this.post("/auth/logout");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Récupère l'utilisateur actuel depuis le cookie
|
|
||||||
*/
|
|
||||||
async getCurrentUser(): Promise<{
|
|
||||||
firstName: string;
|
|
||||||
lastName: string;
|
|
||||||
teamId: string;
|
|
||||||
teamName: string;
|
|
||||||
uuid: string;
|
|
||||||
} | null> {
|
|
||||||
try {
|
|
||||||
const response = await this.get<{
|
|
||||||
user: {
|
|
||||||
firstName: string;
|
|
||||||
lastName: string;
|
|
||||||
teamId: string;
|
|
||||||
teamName: string;
|
|
||||||
uuid: string;
|
|
||||||
};
|
|
||||||
}>("/auth");
|
|
||||||
return response.user;
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to get current user:", error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,13 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import {
|
import { createContext, useContext, useState, ReactNode } from "react";
|
||||||
createContext,
|
|
||||||
useContext,
|
|
||||||
useState,
|
|
||||||
useEffect,
|
|
||||||
ReactNode,
|
|
||||||
} from "react";
|
|
||||||
import { authClient } from "@/clients";
|
|
||||||
|
|
||||||
interface UserInfo {
|
interface UserInfo {
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName: string;
|
||||||
teamName: string;
|
teamName: string;
|
||||||
|
teamId?: string;
|
||||||
|
uuid?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UserContextType {
|
interface UserContextType {
|
||||||
@@ -23,33 +18,14 @@ interface UserContextType {
|
|||||||
|
|
||||||
const UserContext = createContext<UserContextType | undefined>(undefined);
|
const UserContext = createContext<UserContextType | undefined>(undefined);
|
||||||
|
|
||||||
export function UserProvider({ children }: { children: ReactNode }) {
|
interface UserProviderProps {
|
||||||
const [userInfo, setUserInfo] = useState<UserInfo | null>(null);
|
children: ReactNode;
|
||||||
const [loading, setLoading] = useState(true);
|
initialUserInfo: UserInfo | null;
|
||||||
|
|
||||||
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();
|
export function UserProvider({ children, initialUserInfo }: UserProviderProps) {
|
||||||
}, []);
|
const [userInfo, setUserInfo] = useState<UserInfo | null>(initialUserInfo);
|
||||||
|
const [loading, setLoading] = useState(false); // Plus de loading initial
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<UserContext.Provider value={{ userInfo, setUserInfo, loading }}>
|
<UserContext.Provider value={{ userInfo, setUserInfo, loading }}>
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export function middleware(request: NextRequest) {
|
|||||||
const publicPaths = ["/login"];
|
const publicPaths = ["/login"];
|
||||||
|
|
||||||
// Pages API qui ne nécessitent pas d'authentification
|
// Pages API qui ne nécessitent pas d'authentification
|
||||||
const publicApiPaths = ["/api/auth", "/api/teams"];
|
const publicApiPaths = ["/api/teams"];
|
||||||
|
|
||||||
// Vérifier si c'est une route publique
|
// Vérifier si c'est une route publique
|
||||||
if (
|
if (
|
||||||
|
|||||||
Reference in New Issue
Block a user