diff --git a/apps/infoalloggi/package-lock.json b/apps/infoalloggi/package-lock.json index 62faef1..da361c0 100644 --- a/apps/infoalloggi/package-lock.json +++ b/apps/infoalloggi/package-lock.json @@ -58,19 +58,19 @@ "leaflet": "^1.9.4", "leaflet-defaulticon-compatibility": "^0.1.2", "lucide-react": "^0.562.0", - "next": "^16.1.1", + "next": "16.1.1", "next-themes": "^0.4.6", "nextjs-progressbar": "^0.0.16", - "nodemailer": "^7.0.12", + "nodemailer": "7.0.12", "nodemailer-html-to-text": "^3.2.0", "nuqs": "^2.8.6", "pg": "^8.16.3", "postcss": "^8.5.6", "radix-ui": "^1.4.3", - "react": "^19.2.0", + "react": "19.2.0", "react-colorful": "^5.6.1", "react-day-picker": "^9.11.1", - "react-dom": "^19.2.0", + "react-dom": "19.2.0", "react-email": "^5.2.3", "react-hook-form": "^7.71.1", "react-hot-toast": "^2.5.2", @@ -107,8 +107,8 @@ "@types/nodemailer": "^7.0.5", "@types/nodemailer-html-to-text": "^3.1.3", "@types/pg": "^8.16.0", - "@types/react": "^19.2.2", - "@types/react-dom": "^19.2.2", + "@types/react": "19.2.2", + "@types/react-dom": "19.2.2", "@types/react-is": "^19.2.0", "@types/uuid": "^10.0.0", "cross-env": "^10.1.0", @@ -12824,9 +12824,9 @@ } }, "node_modules/react": { - "version": "19.2.3", - "resolved": "https://registry.npmjs.org/react/-/react-19.2.3.tgz", - "integrity": "sha512-Ku/hhYbVjOQnXDZFv2+RibmLFGwFdeeKHFcOTlrt7xplBnya5OGn/hIRDsqDiSUcfORsDC7MPxwork8jBwsIWA==", + "version": "19.2.0", + "resolved": "https://registry.npmjs.org/react/-/react-19.2.0.tgz", + "integrity": "sha512-tmbWg6W31tQLeB5cdIBOicJDJRR2KzXsV7uSK9iNfLWQ5bIZfxuPEHp7M8wiHyHnn0DD1i7w3Zmin0FtkrwoCQ==", "license": "MIT", "peer": true, "engines": { diff --git a/apps/infoalloggi/package.json b/apps/infoalloggi/package.json index aba0191..9198ae6 100644 --- a/apps/infoalloggi/package.json +++ b/apps/infoalloggi/package.json @@ -70,19 +70,19 @@ "leaflet": "^1.9.4", "leaflet-defaulticon-compatibility": "^0.1.2", "lucide-react": "^0.562.0", - "next": "^16.1.1", + "next": "16.1.1", "next-themes": "^0.4.6", "nextjs-progressbar": "^0.0.16", - "nodemailer": "^7.0.12", + "nodemailer": "7.0.12", "nodemailer-html-to-text": "^3.2.0", "nuqs": "^2.8.6", "pg": "^8.16.3", "postcss": "^8.5.6", "radix-ui": "^1.4.3", - "react": "^19.2.0", + "react": "19.2.0", "react-colorful": "^5.6.1", "react-day-picker": "^9.11.1", - "react-dom": "^19.2.0", + "react-dom": "19.2.0", "react-email": "^5.2.3", "react-hook-form": "^7.71.1", "react-hot-toast": "^2.5.2", @@ -119,8 +119,8 @@ "@types/nodemailer": "^7.0.5", "@types/nodemailer-html-to-text": "^3.1.3", "@types/pg": "^8.16.0", - "@types/react": "^19.2.2", - "@types/react-dom": "^19.2.2", + "@types/react": "19.2.2", + "@types/react-dom": "19.2.2", "@types/react-is": "^19.2.0", "@types/uuid": "^10.0.0", "cross-env": "^10.1.0", @@ -134,4 +134,4 @@ "ct3aMetadata": { "initVersion": "7.9.0" } -} +} \ No newline at end of file diff --git a/apps/infoalloggi/src/middleware.ts b/apps/infoalloggi/src/middleware.ts deleted file mode 100644 index de95ed8..0000000 --- a/apps/infoalloggi/src/middleware.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { type NextRequest, NextResponse } from "next/server"; -import { authMiddleware } from "~/middlewares/auth"; -import { apisMiddleware } from "./middlewares/api"; -import { pswChangeMiddleware } from "./middlewares/psw_change"; -import { TestRedirectMiddleware } from "./middlewares/test_redirect"; - -export async function middleware(request: NextRequest) { - //console.log("Middleware triggered for:", request.nextUrl.pathname); - - return await chainMiddlewares(request, [ - TestRedirectMiddleware, - //updaterMiddleware, - apisMiddleware, - pswChangeMiddleware, - authMiddleware, - ]); -} - -export const config = { - matcher: [ - "/storage-api/:path*", - { - source: "/", - }, - { - missing: [ - { key: "next-router-prefetch", type: "header" }, - { key: "purpose", type: "header", value: "prefetch" }, - ], - source: - "/((?!api/trpc|storage-api|api/tiles|api/auth|_next/static|_next/image|screenshots|site.webmanifest|favicon.ico|favicon|sitemap.xml|robots.txt).*)", - }, - ], -}; - -// Type for middleware functions -export type MiddlewareFn = ( - request: NextRequest, -) => Promise | NextResponse | null | undefined; - -// Helper to chain middlewares in order -async function chainMiddlewares( - request: NextRequest, - middlewares: MiddlewareFn[], -): Promise { - for (const middleware of middlewares) { - const result = await middleware(request); - - // If middleware returned a response, short-circuit - if (result) { - return result; - } - // Otherwise, continue to next middleware - } - - // All middlewares passed, continue to the app - return NextResponse.next(); -} diff --git a/apps/infoalloggi/src/middlewares/api.ts b/apps/infoalloggi/src/proxies/api.ts similarity index 98% rename from apps/infoalloggi/src/middlewares/api.ts rename to apps/infoalloggi/src/proxies/api.ts index cc043d8..aa2353f 100644 --- a/apps/infoalloggi/src/middlewares/api.ts +++ b/apps/infoalloggi/src/proxies/api.ts @@ -1,11 +1,11 @@ import { type NextRequest, NextResponse } from "next/server"; import { env } from "~/env"; -import type { MiddlewareFn } from "~/middleware"; +import type { ProxyFn } from "~/proxy"; import { TOKEN_CONFIG } from "~/server/auth/configs"; import { verifyToken } from "~/server/auth/jwt"; const STALE_KEY = "stale-resource"; -export const apisMiddleware: MiddlewareFn = async (req: NextRequest) => { +export const apisProxy: ProxyFn = async (req: NextRequest) => { const { pathname, searchParams } = req.nextUrl; if (!pathname.startsWith("/storage-api/")) { diff --git a/apps/infoalloggi/src/middlewares/auth.ts b/apps/infoalloggi/src/proxies/auth.ts similarity index 97% rename from apps/infoalloggi/src/middlewares/auth.ts rename to apps/infoalloggi/src/proxies/auth.ts index aabbb25..0a73f7d 100644 --- a/apps/infoalloggi/src/middlewares/auth.ts +++ b/apps/infoalloggi/src/proxies/auth.ts @@ -1,10 +1,11 @@ import { add } from "date-fns"; import { type NextRequest, NextResponse } from "next/server"; -import type { MiddlewareFn } from "~/middleware"; +import type { ProxyFn } from "~/proxy"; + import { TOKEN_CONFIG } from "~/server/auth/configs"; import { signToken, verifyToken } from "~/server/auth/jwt"; -export const authMiddleware: MiddlewareFn = async (req: NextRequest) => { +export const authProxy: ProxyFn = async (req: NextRequest) => { const path = req.nextUrl.pathname; const refreshToken = req.cookies.get( diff --git a/apps/infoalloggi/src/middlewares/psw_change.ts b/apps/infoalloggi/src/proxies/psw_change.ts similarity index 82% rename from apps/infoalloggi/src/middlewares/psw_change.ts rename to apps/infoalloggi/src/proxies/psw_change.ts index 913a92f..15668d9 100644 --- a/apps/infoalloggi/src/middlewares/psw_change.ts +++ b/apps/infoalloggi/src/proxies/psw_change.ts @@ -1,9 +1,9 @@ import { type NextRequest, NextResponse } from "next/server"; -import type { MiddlewareFn } from "~/middleware"; +import type { ProxyFn } from "~/proxy"; import { TOKEN_CONFIG } from "~/server/auth/configs"; import { verifyToken } from "~/server/auth/jwt"; -export const pswChangeMiddleware: MiddlewareFn = async (req: NextRequest) => { +export const pswChangeProxy: ProxyFn = async (req: NextRequest) => { if (req.nextUrl.pathname === "/auth/aggiorna-password") { return; } diff --git a/apps/infoalloggi/src/middlewares/test_redirect.ts b/apps/infoalloggi/src/proxies/test_redirect.ts similarity index 68% rename from apps/infoalloggi/src/middlewares/test_redirect.ts rename to apps/infoalloggi/src/proxies/test_redirect.ts index e005757..cdb62ab 100644 --- a/apps/infoalloggi/src/middlewares/test_redirect.ts +++ b/apps/infoalloggi/src/proxies/test_redirect.ts @@ -1,9 +1,8 @@ import { type NextRequest, NextResponse } from "next/server"; import { env } from "~/env"; -import type { MiddlewareFn } from "~/middleware"; -export const TestRedirectMiddleware: MiddlewareFn = async ( - req: NextRequest, -) => { +import type { ProxyFn } from "~/proxy"; + +export const TestRedirectProxy: ProxyFn = async (req: NextRequest) => { const { pathname } = req.nextUrl; if (!pathname.startsWith("/test")) { diff --git a/apps/infoalloggi/src/middlewares/updater.ts b/apps/infoalloggi/src/proxies/updater.ts similarity index 87% rename from apps/infoalloggi/src/middlewares/updater.ts rename to apps/infoalloggi/src/proxies/updater.ts index 1902380..42c1d49 100644 --- a/apps/infoalloggi/src/middlewares/updater.ts +++ b/apps/infoalloggi/src/proxies/updater.ts @@ -1,9 +1,9 @@ import { type NextRequest, NextResponse } from "next/server"; -import type { MiddlewareFn } from "~/middleware"; +import type { ProxyFn } from "~/proxy"; const UPDATE_COODKIE_NAME = "app_version"; -export const updaterMiddleware: MiddlewareFn = async (req: NextRequest) => { +export const updaterProxy: ProxyFn = async (req: NextRequest) => { const url = req.nextUrl.clone(); const updateToken = req.cookies.get(UPDATE_COODKIE_NAME)?.value; diff --git a/apps/infoalloggi/src/proxy.ts b/apps/infoalloggi/src/proxy.ts new file mode 100644 index 0000000..dd2a27c --- /dev/null +++ b/apps/infoalloggi/src/proxy.ts @@ -0,0 +1,58 @@ +import { type NextRequest, NextResponse } from "next/server"; +import { authProxy } from "~/proxies/auth"; +import { apisProxy } from "./proxies/api"; +import { pswChangeProxy } from "./proxies/psw_change"; +import { TestRedirectProxy } from "./proxies/test_redirect"; + +export async function proxy(request: NextRequest) { + //console.log("Proxy triggered for:", request.nextUrl.pathname); + + return await chainProxies(request, [ + TestRedirectProxy, + //updaterProxy, + apisProxy, + pswChangeProxy, + authProxy, + ]); +} + +export const config = { + matcher: [ + "/storage-api/:path*", + { + source: "/", + }, + { + missing: [ + { key: "next-router-prefetch", type: "header" }, + { key: "purpose", type: "header", value: "prefetch" }, + ], + source: + "/((?!api/trpc|storage-api|api/tiles|api/auth|_next/static|_next/image|screenshots|site.webmanifest|favicon.ico|favicon|sitemap.xml|robots.txt).*)", + }, + ], +}; + +// Type for Proxy functions +export type ProxyFn = ( + request: NextRequest, +) => Promise | NextResponse | null | undefined; + +// Helper to chain proxies in order +async function chainProxies( + request: NextRequest, + proxies: ProxyFn[], +): Promise { + for (const proxy of proxies) { + const result = await proxy(request); + + // If proxy returned a response, short-circuit + if (result) { + return result; + } + // Otherwise, continue to next proxy + } + + // All proxies passed, continue to the app + return NextResponse.next(); +}