feat: refactor middleware and proxy structure for improved organization and functionality

- Removed old middleware files and replaced them with new proxy implementations for auth, API, password change, test redirection, and updater functionalities.
- Introduced a new proxy handler to streamline request processing through a chain of proxies.
- Updated package.json and package-lock.json to reflect dependency changes and ensure compatibility with the latest versions of Next.js, React, and related packages.
- Enhanced error handling and response management in the new proxy implementations.
This commit is contained in:
Marco Pedone 2026-01-14 14:51:43 +01:00
parent d79d7c5c6c
commit 2111535d08
9 changed files with 86 additions and 86 deletions

View file

@ -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": {

View file

@ -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",

View file

@ -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> | NextResponse | null | undefined;
// Helper to chain middlewares in order
async function chainMiddlewares(
request: NextRequest,
middlewares: MiddlewareFn[],
): Promise<NextResponse> {
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();
}

View file

@ -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/")) {

View file

@ -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(

View file

@ -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;
}

View file

@ -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")) {

View file

@ -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;

View file

@ -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> | NextResponse | null | undefined;
// Helper to chain proxies in order
async function chainProxies(
request: NextRequest,
proxies: ProxyFn[],
): Promise<NextResponse> {
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();
}