From 43184cd63cfca6902dbd6d38887c9dddb948d73c Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 17 Oct 2025 22:30:51 -0500 Subject: [PATCH] Add detailed production logging in getSession function This commit enhances the getSession function in the authentication middleware by adding comprehensive logging for production environments. It logs session retrieval attempts, cache usage, and session validation results, improving monitoring and debugging capabilities during user authentication processes. --- middlewares/auth/auth.ts | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/middlewares/auth/auth.ts b/middlewares/auth/auth.ts index efe26a49..a4752551 100644 --- a/middlewares/auth/auth.ts +++ b/middlewares/auth/auth.ts @@ -87,11 +87,23 @@ function getCacheKey(request: NextRequest): string { export async function getSession(request: NextRequest, response: NextResponse, forceRefresh = true) { const cacheKey = getCacheKey(request); + const isProduction = process.env.NODE_ENV === 'production'; + + if (isProduction) { + console.log('getSession called:', { + cacheKey, + forceRefresh, + path: request.nextUrl.pathname, + }); + } // Verificar cache si no es forceRefresh if (!forceRefresh) { const cached = sessionCache.get(cacheKey); if (cached) { + if (isProduction) { + console.log('Using cached session'); + } return cached; } } @@ -100,11 +112,26 @@ export async function getSession(request: NextRequest, response: NextResponse, f nextServerContext: { request, response }, operation: async (contextSpec) => { try { + if (isProduction) { + console.log('Fetching auth session from Cognito...'); + } + const session = await fetchAuthSession(contextSpec, { forceRefresh }); const result = session.tokens !== undefined ? session : null; + if (isProduction) { + console.log('Session fetch result:', { + hasSession: !!session, + hasTokens: !!session?.tokens, + tokenTypes: session?.tokens ? Object.keys(session.tokens) : [], + }); + } + // Limpiar caché si la sesión no es válida if (!result || !result.tokens) { + if (isProduction) { + console.log('No valid session found, clearing cache'); + } sessionCache.del(cacheKey); return null; } @@ -112,12 +139,15 @@ export async function getSession(request: NextRequest, response: NextResponse, f // Guardar en cache solo si la sesión es válida sessionCache.set(cacheKey, result); + if (isProduction) { + console.log('Session cached successfully'); + } + return result; } catch (error) { console.error('Error fetching user session:', error); // En producción, ser más permisivo con errores de red/temporales - const isProduction = process.env.NODE_ENV === 'production'; const isNetworkError = error instanceof Error && (error.message.includes('network') ||