From 6b3866e54df7dd0da6bfeb80bf593275881d2d64 Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Fri, 14 Feb 2025 14:35:40 +0100 Subject: [PATCH] refacto(db): get config from mongo everywhere --- src/lib/services/base-api.service.ts | 17 ++++++++++++++--- src/middleware.ts | 27 ++++++++++----------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/lib/services/base-api.service.ts b/src/lib/services/base-api.service.ts index 8716901..ec60c82 100644 --- a/src/lib/services/base-api.service.ts +++ b/src/lib/services/base-api.service.ts @@ -1,15 +1,26 @@ import { cookies } from "next/headers"; import { AuthConfig } from "@/types/auth"; import { serverCacheService } from "./server-cache.service"; -import { komgaConfigService } from "./komga-config.service"; +import { ConfigDBService } from "./config-db.service"; // Types de cache disponibles export type CacheType = "DEFAULT" | "HOME" | "LIBRARIES" | "SERIES" | "BOOKS" | "IMAGES"; export abstract class BaseApiService { protected static async getKomgaConfig(): Promise { - const cookiesStore = cookies(); - return komgaConfigService.validateAndGetConfig(cookiesStore); + try { + const config = await ConfigDBService.getConfig(); + return { + serverUrl: config.url, + credentials: { + username: config.username, + password: config.password, + }, + }; + } catch (error) { + console.error("Erreur lors de la récupération de la configuration:", error); + throw new Error("Configuration Komga non trouvée"); + } } protected static getAuthHeaders(config: AuthConfig): Headers { diff --git a/src/middleware.ts b/src/middleware.ts index 1c651e8..4cc6cff 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,6 +1,5 @@ import { NextResponse } from "next/server"; import type { NextRequest } from "next/server"; -import { komgaConfigService } from "@/lib/services/komga-config.service"; // Routes qui ne nécessitent pas d'authentification const publicRoutes = ["/login", "/register", "/images"]; @@ -20,24 +19,12 @@ export function middleware(request: NextRequest) { return NextResponse.next(); } - // Vérifier si c'est une route d'API - if (pathname.startsWith("/api/")) { - // Vérifier la configuration Komga - const config = komgaConfigService.getConfig(request.cookies); - - if (!komgaConfigService.isConfigValid(config)) { - return NextResponse.json( - { error: "Configuration Komga manquante ou invalide" }, - { status: 401 } - ); - } - - return NextResponse.next(); - } - - // Pour les routes protégées, vérifier la présence de l'utilisateur + // Pour toutes les routes protégées, vérifier la présence de l'utilisateur const user = request.cookies.get("stripUser"); if (!user) { + if (pathname.startsWith("/api/")) { + return NextResponse.json({ error: "Non autorisé" }, { status: 401 }); + } const loginUrl = new URL("/login", request.url); loginUrl.searchParams.set("from", pathname); return NextResponse.redirect(loginUrl); @@ -46,9 +33,15 @@ export function middleware(request: NextRequest) { try { const userData = JSON.parse(atob(user.value)); if (!userData.authenticated) { + if (pathname.startsWith("/api/")) { + return NextResponse.json({ error: "Non autorisé" }, { status: 401 }); + } throw new Error("User not authenticated"); } } catch (error) { + if (pathname.startsWith("/api/")) { + return NextResponse.json({ error: "Non autorisé" }, { status: 401 }); + } const loginUrl = new URL("/login", request.url); loginUrl.searchParams.set("from", pathname); return NextResponse.redirect(loginUrl);