From 99aef49dd94bee7ebbf5d596b556c6631cfa267b Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Wed, 12 Feb 2025 22:12:37 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20am=C3=A9lioration=20de=20la=20gestion=20?= =?UTF-8?q?de=20l'authentification=20pour=20la=20PWA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/layout/Sidebar.tsx | 40 ++++++++++++++++++++++------- src/lib/services/storage.service.ts | 17 ++++++++++++ 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/components/layout/Sidebar.tsx b/src/components/layout/Sidebar.tsx index 8ba0b20..ab9b0a6 100644 --- a/src/components/layout/Sidebar.tsx +++ b/src/components/layout/Sidebar.tsx @@ -5,6 +5,7 @@ import { cn } from "@/lib/utils"; import { authService } from "@/lib/services/auth.service"; import { useEffect, useState } from "react"; import { KomgaLibrary } from "@/types/komga"; +import { storageService } from "@/lib/services/storage.service"; interface SidebarProps { isOpen: boolean; @@ -17,39 +18,60 @@ export function Sidebar({ isOpen }: SidebarProps) { const [isLoading, setIsLoading] = useState(true); const [isAuthenticated, setIsAuthenticated] = useState(false); + // Initialiser l'authentification au montage du composant useEffect(() => { - // Vérifier si l'utilisateur est authentifié - const checkAuth = () => { - const isAuth = authService.isAuthenticated(); - setIsAuthenticated(isAuth); - return isAuth; + const initAuth = () => { + const credentials = storageService.getCredentials(); + const user = storageService.getUser(); + console.log("Sidebar - Init Auth:", { hasCredentials: !!credentials, hasUser: !!user }); + if (credentials && user) { + setIsAuthenticated(true); + return true; + } + return false; }; + initAuth(); + }, []); + + // Effet séparé pour charger les bibliothèques + useEffect(() => { const fetchLibraries = async () => { - if (!checkAuth()) { + if (!isAuthenticated) { setIsLoading(false); return; } try { + console.log("Sidebar - Fetching libraries..."); const response = await fetch("/api/komga/libraries"); if (!response.ok) { - throw new Error("Erreur lors de la récupération des bibliothèques"); + throw new Error(`Erreur ${response.status} lors de la récupération des bibliothèques`); } const data = await response.json(); + console.log("Sidebar - Libraries fetched:", data.length); setLibraries(data); } catch (error) { - console.error("Erreur:", error); + console.error("Sidebar - Error fetching libraries:", error); + if ( + error instanceof Error && + (error.message.includes("401") || error.message.includes("403")) + ) { + console.log("Sidebar - Auth error, logging out"); + handleLogout(); + } } finally { setIsLoading(false); } }; fetchLibraries(); - }, [pathname]); // Recharger quand le pathname change pour gérer la déconnexion + }, [isAuthenticated, pathname]); const handleLogout = () => { + console.log("Sidebar - Logging out"); authService.logout(); + storageService.clearAll(); // Nettoyer tout le stockage setIsAuthenticated(false); setLibraries([]); router.push("/login"); diff --git a/src/lib/services/storage.service.ts b/src/lib/services/storage.service.ts index 9ed251f..17a89ca 100644 --- a/src/lib/services/storage.service.ts +++ b/src/lib/services/storage.service.ts @@ -142,6 +142,23 @@ class StorageService { document.cookie = `${CREDENTIALS_KEY}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`; document.cookie = `${USER_KEY}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`; } + + getUser() { + try { + const userStr = localStorage.getItem("komgaUser"); + if (!userStr) return null; + return JSON.parse(atob(userStr)); + } catch (error) { + console.error("Erreur lors de la récupération de l'utilisateur:", error); + return null; + } + } + + clearAll() { + localStorage.removeItem("komgaUser"); + localStorage.removeItem("komgaCredentials"); + localStorage.removeItem("ttlConfig"); + } } export const storageService = StorageService.getInstance();