fix: ne pas charger les bibliothèques dans la sidebar en mode déconnecté

This commit is contained in:
Julien Froidefond
2025-02-12 15:51:57 +01:00
parent a4d0848334
commit b7cb466ef5

View File

@@ -15,9 +15,22 @@ export function Sidebar({ isOpen }: SidebarProps) {
const router = useRouter(); const router = useRouter();
const [libraries, setLibraries] = useState<KomgaLibrary[]>([]); const [libraries, setLibraries] = useState<KomgaLibrary[]>([]);
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const [isAuthenticated, setIsAuthenticated] = useState(false);
useEffect(() => { useEffect(() => {
// Vérifier si l'utilisateur est authentifié
const checkAuth = () => {
const isAuth = authService.isAuthenticated();
setIsAuthenticated(isAuth);
return isAuth;
};
const fetchLibraries = async () => { const fetchLibraries = async () => {
if (!checkAuth()) {
setIsLoading(false);
return;
}
try { try {
const response = await fetch("/api/komga/libraries"); const response = await fetch("/api/komga/libraries");
if (!response.ok) { if (!response.ok) {
@@ -33,10 +46,12 @@ export function Sidebar({ isOpen }: SidebarProps) {
}; };
fetchLibraries(); fetchLibraries();
}, []); }, [pathname]); // Recharger quand le pathname change pour gérer la déconnexion
const handleLogout = () => { const handleLogout = () => {
authService.logout(); authService.logout();
setIsAuthenticated(false);
setLibraries([]);
router.push("/login"); router.push("/login");
}; };
@@ -78,6 +93,8 @@ export function Sidebar({ isOpen }: SidebarProps) {
</div> </div>
</div> </div>
{isAuthenticated && (
<>
<div className="px-3 py-2"> <div className="px-3 py-2">
<div className="space-y-1"> <div className="space-y-1">
<h2 className="mb-2 px-4 text-lg font-semibold tracking-tight">Bibliothèques</h2> <h2 className="mb-2 px-4 text-lg font-semibold tracking-tight">Bibliothèques</h2>
@@ -118,9 +135,11 @@ export function Sidebar({ isOpen }: SidebarProps) {
</Link> </Link>
</div> </div>
</div> </div>
</>
)}
</div> </div>
{/* Bouton de déconnexion */} {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}
@@ -130,6 +149,7 @@ export function Sidebar({ isOpen }: SidebarProps) {
Se déconnecter Se déconnecter
</button> </button>
</div> </div>
)}
</aside> </aside>
); );
} }