fix(Sidebar): not shown on public route
This commit is contained in:
@@ -6,9 +6,26 @@ import { Header } from "@/components/layout/Header";
|
|||||||
import { Sidebar } from "@/components/layout/Sidebar";
|
import { Sidebar } from "@/components/layout/Sidebar";
|
||||||
import { InstallPWA } from "../ui/InstallPWA";
|
import { InstallPWA } from "../ui/InstallPWA";
|
||||||
import { Toaster } from "@/components/ui/toaster";
|
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 }) {
|
export default function ClientLayout({ children }: { children: React.ReactNode }) {
|
||||||
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
|
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
|
// Gestionnaire pour fermer la barre latérale lors d'un clic en dehors
|
||||||
useEffect(() => {
|
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 (
|
return (
|
||||||
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
|
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
|
||||||
<div className="relative min-h-screen">
|
<div className="relative min-h-screen">
|
||||||
<Header onToggleSidebar={() => setIsSidebarOpen(!isSidebarOpen)} />
|
{!isPublicRoute && <Header onToggleSidebar={() => setIsSidebarOpen(!isSidebarOpen)} />}
|
||||||
<Sidebar isOpen={isSidebarOpen} />
|
{!isPublicRoute && <Sidebar isOpen={isSidebarOpen} />}
|
||||||
<main className="container pt-4 md:pt-8">{children}</main>
|
<main className={`${!isPublicRoute ? "container pt-4 md:pt-8" : ""}`}>{children}</main>
|
||||||
<InstallPWA />
|
<InstallPWA />
|
||||||
<Toaster />
|
<Toaster />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -17,26 +17,10 @@ export function Sidebar({ isOpen }: SidebarProps) {
|
|||||||
const [libraries, setLibraries] = useState<KomgaLibrary[]>([]);
|
const [libraries, setLibraries] = useState<KomgaLibrary[]>([]);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||||
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
||||||
|
|
||||||
// Initialiser l'authentification au montage du composant
|
// 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 () => {
|
const fetchLibraries = useCallback(async () => {
|
||||||
if (!isAuthenticated) return;
|
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
console.log("Sidebar - Fetching libraries...");
|
console.log("Sidebar - Fetching libraries...");
|
||||||
try {
|
try {
|
||||||
@@ -54,11 +38,11 @@ export function Sidebar({ isOpen }: SidebarProps) {
|
|||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
setIsRefreshing(false);
|
setIsRefreshing(false);
|
||||||
}
|
}
|
||||||
}, [isAuthenticated]);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchLibraries();
|
fetchLibraries();
|
||||||
}, [isAuthenticated, pathname, fetchLibraries]);
|
}, [pathname, fetchLibraries]);
|
||||||
|
|
||||||
const handleRefresh = async () => {
|
const handleRefresh = async () => {
|
||||||
setIsRefreshing(true);
|
setIsRefreshing(true);
|
||||||
@@ -68,8 +52,7 @@ export function Sidebar({ isOpen }: SidebarProps) {
|
|||||||
const handleLogout = () => {
|
const handleLogout = () => {
|
||||||
console.log("Sidebar - Logging out");
|
console.log("Sidebar - Logging out");
|
||||||
authService.logout();
|
authService.logout();
|
||||||
storageService.clearAll(); // Nettoyer tout le stockage
|
storageService.clearAll();
|
||||||
setIsAuthenticated(false);
|
|
||||||
setLibraries([]);
|
setLibraries([]);
|
||||||
router.push("/login");
|
router.push("/login");
|
||||||
};
|
};
|
||||||
@@ -112,73 +95,67 @@ export function Sidebar({ isOpen }: SidebarProps) {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{isAuthenticated && (
|
<div className="px-3 py-2">
|
||||||
<>
|
<div className="space-y-1">
|
||||||
<div className="px-3 py-2">
|
<div className="mb-2 px-4 flex items-center justify-between">
|
||||||
<div className="space-y-1">
|
<h2 className="text-lg font-semibold tracking-tight">Bibliothèques</h2>
|
||||||
<div className="mb-2 px-4 flex items-center justify-between">
|
<button
|
||||||
<h2 className="text-lg font-semibold tracking-tight">Bibliothèques</h2>
|
onClick={handleRefresh}
|
||||||
<button
|
disabled={isRefreshing}
|
||||||
onClick={handleRefresh}
|
className="p-1 hover:bg-accent hover:text-accent-foreground rounded-md transition-colors"
|
||||||
disabled={isRefreshing}
|
aria-label="Rafraîchir les bibliothèques"
|
||||||
className="p-1 hover:bg-accent hover:text-accent-foreground rounded-md transition-colors"
|
>
|
||||||
aria-label="Rafraîchir les bibliothèques"
|
<RefreshCw className={cn("h-4 w-4", isRefreshing && "animate-spin")} />
|
||||||
>
|
</button>
|
||||||
<RefreshCw className={cn("h-4 w-4", isRefreshing && "animate-spin")} />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
{isLoading ? (
|
|
||||||
<div className="px-3 py-2 text-sm text-muted-foreground">Chargement...</div>
|
|
||||||
) : libraries.length === 0 ? (
|
|
||||||
<div className="px-3 py-2 text-sm text-muted-foreground">Aucune bibliothèque</div>
|
|
||||||
) : (
|
|
||||||
libraries.map((library) => (
|
|
||||||
<Link
|
|
||||||
key={library.id}
|
|
||||||
href={`/libraries/${library.id}`}
|
|
||||||
className={cn(
|
|
||||||
"flex items-center rounded-lg px-3 py-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground",
|
|
||||||
pathname === `/libraries/${library.id}` ? "bg-accent" : "transparent"
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<Library className="mr-2 h-4 w-4" />
|
|
||||||
{library.name}
|
|
||||||
</Link>
|
|
||||||
))
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
{isLoading ? (
|
||||||
<div className="px-3 py-2">
|
<div className="px-3 py-2 text-sm text-muted-foreground">Chargement...</div>
|
||||||
<div className="space-y-1">
|
) : libraries.length === 0 ? (
|
||||||
<h2 className="mb-2 px-4 text-lg font-semibold tracking-tight">Configuration</h2>
|
<div className="px-3 py-2 text-sm text-muted-foreground">Aucune bibliothèque</div>
|
||||||
|
) : (
|
||||||
|
libraries.map((library) => (
|
||||||
<Link
|
<Link
|
||||||
href="/settings"
|
key={library.id}
|
||||||
|
href={`/libraries/${library.id}`}
|
||||||
className={cn(
|
className={cn(
|
||||||
"flex items-center rounded-lg px-3 py-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground",
|
"flex items-center rounded-lg px-3 py-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground",
|
||||||
pathname === "/settings" ? "bg-accent" : "transparent"
|
pathname === `/libraries/${library.id}` ? "bg-accent" : "transparent"
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<Settings className="mr-2 h-4 w-4" />
|
<Library className="mr-2 h-4 w-4" />
|
||||||
Préférences
|
{library.name}
|
||||||
</Link>
|
</Link>
|
||||||
</div>
|
))
|
||||||
</div>
|
)}
|
||||||
</>
|
</div>
|
||||||
)}
|
</div>
|
||||||
|
|
||||||
|
<div className="px-3 py-2">
|
||||||
|
<div className="space-y-1">
|
||||||
|
<h2 className="mb-2 px-4 text-lg font-semibold tracking-tight">Configuration</h2>
|
||||||
|
<Link
|
||||||
|
href="/settings"
|
||||||
|
className={cn(
|
||||||
|
"flex items-center rounded-lg px-3 py-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground",
|
||||||
|
pathname === "/settings" ? "bg-accent" : "transparent"
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<Settings className="mr-2 h-4 w-4" />
|
||||||
|
Préférences
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{isAuthenticated && (
|
<div className="p-3 border-t border-border/40">
|
||||||
<div className="p-3 border-t border-border/40">
|
<button
|
||||||
<button
|
onClick={handleLogout}
|
||||||
onClick={handleLogout}
|
className="flex w-full items-center rounded-lg px-3 py-2 text-sm font-medium text-destructive hover:bg-destructive/10 hover:text-destructive"
|
||||||
className="flex w-full items-center rounded-lg px-3 py-2 text-sm font-medium text-destructive hover:bg-destructive/10 hover:text-destructive"
|
>
|
||||||
>
|
<LogOut className="mr-2 h-4 w-4" />
|
||||||
<LogOut className="mr-2 h-4 w-4" />
|
Se déconnecter
|
||||||
Se déconnecter
|
</button>
|
||||||
</button>
|
</div>
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</aside>
|
</aside>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user