refacto(db): get config from mongo everywhere

This commit is contained in:
Julien Froidefond
2025-02-14 14:35:40 +01:00
parent 1f881ade26
commit 6b3866e54d
2 changed files with 24 additions and 20 deletions

View File

@@ -1,15 +1,26 @@
import { cookies } from "next/headers"; import { cookies } from "next/headers";
import { AuthConfig } from "@/types/auth"; import { AuthConfig } from "@/types/auth";
import { serverCacheService } from "./server-cache.service"; import { serverCacheService } from "./server-cache.service";
import { komgaConfigService } from "./komga-config.service"; import { ConfigDBService } from "./config-db.service";
// Types de cache disponibles // Types de cache disponibles
export type CacheType = "DEFAULT" | "HOME" | "LIBRARIES" | "SERIES" | "BOOKS" | "IMAGES"; export type CacheType = "DEFAULT" | "HOME" | "LIBRARIES" | "SERIES" | "BOOKS" | "IMAGES";
export abstract class BaseApiService { export abstract class BaseApiService {
protected static async getKomgaConfig(): Promise<AuthConfig> { protected static async getKomgaConfig(): Promise<AuthConfig> {
const cookiesStore = cookies(); try {
return komgaConfigService.validateAndGetConfig(cookiesStore); 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 { protected static getAuthHeaders(config: AuthConfig): Headers {

View File

@@ -1,6 +1,5 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import type { NextRequest } 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 // Routes qui ne nécessitent pas d'authentification
const publicRoutes = ["/login", "/register", "/images"]; const publicRoutes = ["/login", "/register", "/images"];
@@ -20,24 +19,12 @@ export function middleware(request: NextRequest) {
return NextResponse.next(); return NextResponse.next();
} }
// Vérifier si c'est une route d'API // Pour toutes les routes protégées, vérifier la présence de l'utilisateur
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
const user = request.cookies.get("stripUser"); const user = request.cookies.get("stripUser");
if (!user) { if (!user) {
if (pathname.startsWith("/api/")) {
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
}
const loginUrl = new URL("/login", request.url); const loginUrl = new URL("/login", request.url);
loginUrl.searchParams.set("from", pathname); loginUrl.searchParams.set("from", pathname);
return NextResponse.redirect(loginUrl); return NextResponse.redirect(loginUrl);
@@ -46,9 +33,15 @@ export function middleware(request: NextRequest) {
try { try {
const userData = JSON.parse(atob(user.value)); const userData = JSON.parse(atob(user.value));
if (!userData.authenticated) { if (!userData.authenticated) {
if (pathname.startsWith("/api/")) {
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
}
throw new Error("User not authenticated"); throw new Error("User not authenticated");
} }
} catch (error) { } catch (error) {
if (pathname.startsWith("/api/")) {
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
}
const loginUrl = new URL("/login", request.url); const loginUrl = new URL("/login", request.url);
loginUrl.searchParams.set("from", pathname); loginUrl.searchParams.set("from", pathname);
return NextResponse.redirect(loginUrl); return NextResponse.redirect(loginUrl);