From 62ea837a0f36cc6435583fad7a8c2ef2be7d9322 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 17 Oct 2025 18:47:25 -0500 Subject: [PATCH] Add production logging for login and authentication redirects This commit enhances the middleware and authentication handlers by adding logging functionality specifically for the production environment. It captures details such as request paths and timestamps during login redirects and session checks, improving the ability to monitor and debug user authentication flows in a live setting. --- middleware.ts | 8 ++++++++ middlewares/auth/auth.ts | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/middleware.ts b/middleware.ts index fba27c4e..e268f55e 100644 --- a/middleware.ts +++ b/middleware.ts @@ -348,7 +348,15 @@ async function handleLoginRedirect( request: NextRequest, next: () => Promise ): Promise { + const isProduction = process.env.NODE_ENV === 'production'; + if (request.nextUrl.pathname === PROTECTED_ROUTES.LOGIN) { + if (isProduction) { + console.log('Login redirect handler called:', { + path: request.nextUrl.pathname, + timestamp: new Date().toISOString(), + }); + } return await handleAuthenticatedRedirectMiddleware(request, NextResponse.next()); } diff --git a/middlewares/auth/auth.ts b/middlewares/auth/auth.ts index e1221690..efe26a49 100644 --- a/middlewares/auth/auth.ts +++ b/middlewares/auth/auth.ts @@ -181,11 +181,24 @@ export async function handleAuthenticationMiddleware(request: NextRequest, respo } export async function handleAuthenticatedRedirectMiddleware(request: NextRequest, response: NextResponse) { + const isProduction = process.env.NODE_ENV === 'production'; + + if (isProduction) { + console.log('Authenticated redirect middleware called:', { + path: request.nextUrl.pathname, + timestamp: new Date().toISOString(), + }); + } + // Siempre forzar refresh para verificar la sesión actual, especialmente importante // cuando el usuario navega manualmente a /login const session = await getSession(request, response, true); if (session && typeof session === 'object' && 'tokens' in session && session.tokens) { + if (isProduction) { + console.log('User has valid session, redirecting...'); + } + // Verificar que la sesión tiene tokens válidos antes de redirigir const lastStoreId = getLastVisitedStore(request); @@ -196,6 +209,10 @@ export async function handleAuthenticatedRedirectMiddleware(request: NextRequest } } + if (isProduction) { + console.log('No valid session found, allowing login page'); + } + // Si no hay sesión válida, limpiar caché y permitir continuar (mostrar login) clearUserSessionCache(request); return response;