diff --git a/src/app/api/analyze/route.ts b/src/app/api/analyze/route.ts index dde9aeb..0f5d997 100644 --- a/src/app/api/analyze/route.ts +++ b/src/app/api/analyze/route.ts @@ -146,19 +146,25 @@ export async function POST(req: NextRequest): Promise { const formData = await req.formData() const imageFile = formData.get('image') - // Check for wallet address in form data - const walletAddress = formData.get('walletAddress'); + // Check for wallet address in both form data and headers + let walletAddress = formData.get('walletAddress'); + + // Also check custom header (this is more reliable with Next.js) + const walletHeaderAddress = req.headers.get('X-Wallet-Address'); + if (walletHeaderAddress && !walletAddress) { + console.log('Found wallet address in custom header:', walletHeaderAddress); + walletAddress = walletHeaderAddress; + } // Log more detailed data about the wallet address - console.log('Form data received:', { + console.log('Request data received:', { hasImage: !!imageFile, imageType: imageFile ? typeof imageFile : 'undefined', - hasWalletAddress: !!walletAddress, - walletAddressType: typeof walletAddress, - walletAddress: walletAddress || 'Not provided', - formDataKeys: Array.from(formData.keys()), - // Log all form data for debugging - formData: Object.fromEntries(formData.entries()) + hasWalletAddressInForm: !!formData.get('walletAddress'), + hasWalletAddressInHeader: !!walletHeaderAddress, + finalWalletAddress: walletAddress || 'Not provided', + headers: Object.fromEntries(req.headers.entries()), + formDataKeys: Array.from(formData.keys()) }); // For server-side, check if we have a valid Blob-like object @@ -380,36 +386,22 @@ export async function POST(req: NextRequest): Promise { // AFTER points are awarded, try to award tokens if wallet is connected // This is separate so issues with token rewards don't affect points try { - // Check all form data keys and values for debugging - const formDataEntries = Array.from(formData.entries()); - console.log('All form data entries:', formDataEntries.map(entry => ({ - key: entry[0], - value: entry[1], - valueType: typeof entry[1] - }))); + // We already extracted the wallet address at the beginning of the request + // walletAddress is already defined in the outer scope - // Check if wallet address was provided in the form data - const walletAddressRaw = formData.get('walletAddress'); - // Ensure we get a string value - formData can have different types - const walletAddress = walletAddressRaw ? String(walletAddressRaw) : null; + // Ensure it's a string type + const walletAddressString = walletAddress ? String(walletAddress) : null; - // More verbose debugging - console.log('Wallet address detailed extraction:', { - walletAddressRaw, - typeOfRaw: typeof walletAddressRaw, - isNull: walletAddressRaw === null, - isUndefined: walletAddressRaw === undefined, - isEmptyString: walletAddressRaw === '', + console.log('Wallet address for token award (final check):', { walletAddress, - hasWalletChecked: formData.has('walletChecked'), - walletStatus: formData.get('walletStatus') + walletAddressString, + walletAddressType: typeof walletAddress }); // Log the wallet address for debugging console.log('Wallet address for token award:', { - rawValue: walletAddressRaw, processedValue: walletAddress || 'Not provided', - valueType: typeof walletAddressRaw + valueType: typeof walletAddress }); // Import dynamically to avoid server-side issues with window object @@ -434,7 +426,12 @@ export async function POST(req: NextRequest): Promise { console.log('Starting token award with explicit address:', walletAddress); // Attempt to award tokens for this wildlife sighting - const tokenResult = await awardTokensForSighting(species, undefined, walletAddress); + // Make sure we're passing a properly processed string + const tokenResult = await awardTokensForSighting( + species, + undefined, + walletAddressString // Use the explicitly string-converted version + ); // Add token reward info to response if successful if (tokenResult.success) { diff --git a/src/components/WildlifeIdentifier.tsx b/src/components/WildlifeIdentifier.tsx index 704de0d..ab5863c 100644 --- a/src/components/WildlifeIdentifier.tsx +++ b/src/components/WildlifeIdentifier.tsx @@ -110,9 +110,19 @@ const WildlifeIdentifier: React.FC = () => { console.log('Wallet connection status:', isWalletConnected()); console.log('User wallet address:', userAddress || 'Not connected'); - // Send to the Next.js API route + // Prepare custom headers including wallet address + const headers = new Headers(); + + // Add wallet address as a custom header if available + if (userAddress) { + console.log('Adding wallet address to custom header:', userAddress); + headers.append('X-Wallet-Address', userAddress); + } + + // Send to the Next.js API route with custom headers const response = await fetch('/api/analyze', { method: 'POST', + headers: headers, body: formData });