From 2142bd7c5dbf6900ff2f70c665eb371fe763d3ef Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 17 Oct 2025 18:21:02 -0500 Subject: [PATCH] Add logging for production environment in middleware and authentication handlers This commit introduces logging functionality in the main middleware and authentication middleware to capture entry points and session status in production. The logs include request path, method, and timestamps, enhancing the ability to debug issues in a live environment while maintaining existing validation checks for Amplify configuration and session management. --- lib/debug/auth-debug.ts | 2 ++ middleware.ts | 11 +++++++++++ middlewares/auth/auth.ts | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/debug/auth-debug.ts b/lib/debug/auth-debug.ts index c354e8ed..f9e60620 100644 --- a/lib/debug/auth-debug.ts +++ b/lib/debug/auth-debug.ts @@ -23,6 +23,8 @@ import outputs from '@/amplify_outputs.json'; export function debugAuthIssues(request: NextRequest) { const isProduction = process.env.NODE_ENV === 'production'; + console.log('debugAuthIssues called', { isProduction, path: request.nextUrl.pathname }); + if (!isProduction) { return null; } diff --git a/middleware.ts b/middleware.ts index e09eaae1..fba27c4e 100644 --- a/middleware.ts +++ b/middleware.ts @@ -394,6 +394,17 @@ async function executeHandlers( * @returns Respuesta procesada por los middlewares */ export async function middleware(request: NextRequest): Promise { + const isProduction = process.env.NODE_ENV === 'production'; + + // Log de entrada del middleware principal + if (isProduction) { + console.log('Main middleware called:', { + path: request.nextUrl.pathname, + method: request.method, + timestamp: new Date().toISOString(), + }); + } + // Definir la cadena de handlers en orden de ejecución const handlers: MiddlewareHandler[] = [ handleOAuthProtection, diff --git a/middlewares/auth/auth.ts b/middlewares/auth/auth.ts index 0f366fc9..e1221690 100644 --- a/middlewares/auth/auth.ts +++ b/middlewares/auth/auth.ts @@ -143,8 +143,18 @@ export async function getSession(request: NextRequest, response: NextResponse, f } export async function handleAuthenticationMiddleware(request: NextRequest, response: NextResponse) { - // Validar configuración de Amplify en producción const isProduction = process.env.NODE_ENV === 'production'; + + // Log de entrada para debugging + if (isProduction) { + console.log('Auth middleware called:', { + path: request.nextUrl.pathname, + method: request.method, + timestamp: new Date().toISOString(), + }); + } + + // Validar configuración de Amplify en producción if (isProduction && !validateAmplifyConfig()) { console.error('Invalid Amplify configuration detected'); } @@ -154,6 +164,7 @@ export async function handleAuthenticationMiddleware(request: NextRequest, respo if (!session) { // Debug detallado en producción if (isProduction) { + console.log('No session found, running debug...'); debugAuthIssues(request); } @@ -162,6 +173,10 @@ export async function handleAuthenticationMiddleware(request: NextRequest, respo return NextResponse.redirect(new URL('/login', request.url), { status: 302 }); } + if (isProduction) { + console.log('Session found, continuing...'); + } + return null; // Permitir que el middleware continúe }