diff --git a/src/components/layout/ClientLayout.tsx b/src/components/layout/ClientLayout.tsx index 8d7238c..3cbf9e8 100644 --- a/src/components/layout/ClientLayout.tsx +++ b/src/components/layout/ClientLayout.tsx @@ -6,9 +6,26 @@ import { Header } from "@/components/layout/Header"; import { Sidebar } from "@/components/layout/Sidebar"; import { InstallPWA } from "../ui/InstallPWA"; import { Toaster } from "@/components/ui/toaster"; +import { usePathname, useRouter } from "next/navigation"; +import { authService } from "@/lib/services/auth.service"; + +// Routes qui ne nécessitent pas d'authentification +const publicRoutes = ["/login", "/register"]; export default function ClientLayout({ children }: { children: React.ReactNode }) { const [isSidebarOpen, setIsSidebarOpen] = useState(false); + const router = useRouter(); + const pathname = usePathname(); + + // Vérification de l'authentification + useEffect(() => { + const isPublicRoute = publicRoutes.includes(pathname); + const isAuthenticated = authService.isAuthenticated(); + + if (!isAuthenticated && !isPublicRoute) { + router.push(`/login?from=${encodeURIComponent(pathname)}`); + } + }, [pathname, router]); // Gestionnaire pour fermer la barre latérale lors d'un clic en dehors useEffect(() => { @@ -49,12 +66,15 @@ export default function ClientLayout({ children }: { children: React.ReactNode } } }, []); + // Ne pas afficher le header et la sidebar sur les routes publiques + const isPublicRoute = publicRoutes.includes(pathname); + return (
-
setIsSidebarOpen(!isSidebarOpen)} /> - -
{children}
+ {!isPublicRoute &&
setIsSidebarOpen(!isSidebarOpen)} />} + {!isPublicRoute && } +
{children}
diff --git a/src/components/layout/Sidebar.tsx b/src/components/layout/Sidebar.tsx index 9caec95..fd9eb32 100644 --- a/src/components/layout/Sidebar.tsx +++ b/src/components/layout/Sidebar.tsx @@ -17,26 +17,10 @@ export function Sidebar({ isOpen }: SidebarProps) { const [libraries, setLibraries] = useState([]); const [isLoading, setIsLoading] = useState(true); const [isRefreshing, setIsRefreshing] = useState(false); - const [isAuthenticated, setIsAuthenticated] = useState(false); // Initialiser l'authentification au montage du composant - useEffect(() => { - 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(); - }, []); const fetchLibraries = useCallback(async () => { - if (!isAuthenticated) return; setIsLoading(true); console.log("Sidebar - Fetching libraries..."); try { @@ -54,11 +38,11 @@ export function Sidebar({ isOpen }: SidebarProps) { setIsLoading(false); setIsRefreshing(false); } - }, [isAuthenticated]); + }, []); useEffect(() => { fetchLibraries(); - }, [isAuthenticated, pathname, fetchLibraries]); + }, [pathname, fetchLibraries]); const handleRefresh = async () => { setIsRefreshing(true); @@ -68,8 +52,7 @@ export function Sidebar({ isOpen }: SidebarProps) { const handleLogout = () => { console.log("Sidebar - Logging out"); authService.logout(); - storageService.clearAll(); // Nettoyer tout le stockage - setIsAuthenticated(false); + storageService.clearAll(); setLibraries([]); router.push("/login"); }; @@ -112,73 +95,67 @@ export function Sidebar({ isOpen }: SidebarProps) { - {isAuthenticated && ( - <> -
-
-
-

Bibliothèques

- -
- {isLoading ? ( -
Chargement...
- ) : libraries.length === 0 ? ( -
Aucune bibliothèque
- ) : ( - libraries.map((library) => ( - - - {library.name} - - )) - )} -
+
+
+
+

Bibliothèques

+
- -
-
-

Configuration

+ {isLoading ? ( +
Chargement...
+ ) : libraries.length === 0 ? ( +
Aucune bibliothèque
+ ) : ( + libraries.map((library) => ( - - Préférences + + {library.name} -
-
- - )} + )) + )} +
+
+ +
+
+

Configuration

+ + + Préférences + +
+
- {isAuthenticated && ( -
- -
- )} +
+ +
); }