From abd0de9f121874a05441e123504d88339664ee44 Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Mon, 25 Aug 2025 19:14:56 +0200 Subject: [PATCH] feat: add logout functionality to navigation component - Integrated logout feature in the navigation dropdown using authClient. - Implemented toast notifications for successful logout and error handling. - Redirected users to the login page post-logout for improved user experience. --- app/api/auth/route.ts | 62 ++------------------------------ components/layout/navigation.tsx | 36 +++++++++++++++---- 2 files changed, 33 insertions(+), 65 deletions(-) diff --git a/app/api/auth/route.ts b/app/api/auth/route.ts index bfa4e59..0f998d4 100644 --- a/app/api/auth/route.ts +++ b/app/api/auth/route.ts @@ -1,6 +1,9 @@ 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 @@ -51,62 +54,3 @@ export async function GET(request: NextRequest) { ); } } - -/** - * 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 }); - } -} diff --git a/components/layout/navigation.tsx b/components/layout/navigation.tsx index 113894a..0ad39fd 100644 --- a/components/layout/navigation.tsx +++ b/components/layout/navigation.tsx @@ -1,7 +1,7 @@ "use client"; import Link from "next/link"; -import { usePathname } from "next/navigation"; +import { usePathname, useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { ThemeToggle } from "@/components/layout/theme-toggle"; import { @@ -17,6 +17,8 @@ import { DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; +import { authClient } from "@/clients"; +import { useToast } from "@/hooks/use-toast"; interface NavigationProps { userInfo?: { @@ -28,6 +30,27 @@ interface NavigationProps { export function Navigation({ userInfo }: NavigationProps = {}) { const pathname = usePathname(); + const router = useRouter(); + const { toast } = useToast(); + + const handleLogout = async () => { + try { + await authClient.logout(); + toast({ + title: "Déconnexion réussie", + description: "Vous avez été déconnecté avec succès.", + }); + // Rediriger vers la page de login après déconnexion + router.push("/login"); + } catch (error: any) { + console.error("Logout failed:", error); + toast({ + title: "Erreur de déconnexion", + description: "Erreur lors de la déconnexion. Veuillez réessayer.", + variant: "destructive", + }); + } + }; const navItems = [ { @@ -106,11 +129,12 @@ export function Navigation({ userInfo }: NavigationProps = {}) { Mon compte - - - - Se déconnecter - + + + Se déconnecter