From 3214322e1be0746aa0d84d4b0941a02798c15757 Mon Sep 17 00:00:00 2001 From: zramsay Date: Tue, 11 Mar 2025 17:38:40 -0400 Subject: [PATCH] more auth fixes --- src/app/api/analyze/route.ts | 16 +++++++++++++--- src/app/page.tsx | 14 ++++++++++++++ src/lib/auth.ts | 18 ++++++++++++++---- src/services/googleVisionService.ts | 13 +++++++++++-- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/app/api/analyze/route.ts b/src/app/api/analyze/route.ts index d27db48..dfacdba 100644 --- a/src/app/api/analyze/route.ts +++ b/src/app/api/analyze/route.ts @@ -24,6 +24,15 @@ export async function POST(req: NextRequest): Promise { // Check authentication - accept session OR header authentication const isAuthenticated = !!session || !!headerUser; + // Log authentication attempt details + console.log('Authentication attempt:', { + hasCookie: !!req.cookies.get('next-auth.session-token') || !!req.cookies.get('__Secure-next-auth.session-token'), + hasSession: !!session, + hasHeaderUser: !!headerUser, + cookies: Array.from(req.cookies.getAll()).map(c => ({ name: c.name, value: c.name.includes('next-auth') ? '[REDACTED]' : c.value })), + allHeaders: Object.fromEntries(Array.from(req.headers.entries())), + }); + if (!isAuthenticated) { console.log('Unauthorized access attempt to analyze API'); return NextResponse.json( @@ -32,9 +41,10 @@ export async function POST(req: NextRequest): Promise { ); } - // Determine user info from either source - const userId = headerUser?.id; - const userEmail = headerUser?.email; + // Use headers for user info if available, otherwise fall back to session + // This is likely our issue - we were checking for a session but not extracting user info from it + const userId = headerUser?.id || req.headers.get('x-user-id') || 'unknown'; + const userEmail = headerUser?.email || req.headers.get('x-user-email') || 'unknown@example.com'; // Log authentication details console.log('Authentication details:', { diff --git a/src/app/page.tsx b/src/app/page.tsx index 2f72992..3b22e8c 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -13,6 +13,20 @@ const Page: React.FC = (): React.ReactElement => { // Get auth session const { data: session, status } = useSession() const isAuthenticated = status === 'authenticated' && !!session + + // Log session details for debugging + React.useEffect(() => { + if (session) { + console.log('Session available:', { + authenticated: isAuthenticated, + user: session.user ? { + name: session.user.name, + email: session.user.email, + id: session.user.id + } : 'No user data' + }); + } + }, [session, isAuthenticated]); const theme = getThemeColors(APP_CONFIG.theme) diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 60b2516..dbaf16c 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -5,17 +5,27 @@ import NextAuth from "next-auth"; // Helper to get basic authentication status from the cookie export async function getSessionFromCookie(req: NextRequest) { try { - // Basic check for auth cookie + // Log all cookies for debugging + console.log('All cookies:', Array.from(req.cookies.getAll()).map(c => c.name)); + + // Look for session cookie - check all possible formats const sessionCookie = req.cookies.get('next-auth.session-token') || - req.cookies.get('__Secure-next-auth.session-token'); + req.cookies.get('__Secure-next-auth.session-token') || + req.cookies.get('__Host-next-auth.session-token'); if (!sessionCookie) { + console.log('No session cookie found'); return null; } - // For now, we'll just check if the cookie exists - we'll get actual user data from headers + console.log('Session cookie found:', sessionCookie.name); + + // In a production setup, we would decode and verify the JWT + // But for simplicity, we'll just check if the cookie exists return { - isAuthenticated: true + isAuthenticated: true, + // In Vercel, the headers will include user info if you're using next-auth + // We'll add fallback user extraction in the API route }; } catch (error) { console.error('Error getting session from cookie:', error); diff --git a/src/services/googleVisionService.ts b/src/services/googleVisionService.ts index ed0fe2f..ce1713b 100644 --- a/src/services/googleVisionService.ts +++ b/src/services/googleVisionService.ts @@ -45,18 +45,27 @@ export async function analyzeImage( } // Prepare headers with user data if available - const userHeaders: Record = {}; + const userHeaders: Record = { + 'Accept': 'application/json' + }; + if (sessionData?.userId) { userHeaders['x-user-id'] = sessionData.userId; + console.log(`Setting user ID header: ${sessionData.userId}`); } if (sessionData?.userEmail) { userHeaders['x-user-email'] = sessionData.userEmail; + console.log(`Setting user email header: ${sessionData.userEmail}`); } + // Log headers for debugging + console.log('Sending request with headers:', userHeaders); + const response = await fetch('/api/analyze', { method: 'POST', headers: userHeaders, - body: formData + body: formData, + credentials: 'include' // Important - include credentials (cookies) }) if (!response.ok) {