This commit is contained in:
zramsay 2025-03-21 21:36:21 -04:00
parent 3f6052e5ed
commit 4a192519ac
2 changed files with 24 additions and 16 deletions

View File

@ -1,65 +1,71 @@
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { getSessionFromCookie, getUserFromHeaders } from '@/lib/auth';
import { awardTokensForSighting } from '@/services/blockchain/tokenRewardService';
import { logger } from '@/services/constants';
export async function POST(req: NextRequest) {
try {
logger.info('award-tokens API called');
console.log('award-tokens API called');
// Log all request headers for debugging
logger.info('Request headers:', Object.fromEntries(req.headers.entries()));
console.log('Request headers:', Object.fromEntries(req.headers.entries()));
// Make authentication optional for demonstration - we'll allow both authenticated and
// unauthenticated requests for testing purposes
let userEmail = 'unknown@example.com';
try {
const session = await getServerSession(authOptions);
// Try to get session from cookie first
const session = await getSessionFromCookie(req);
// Also try header-based authentication
const headerUser = getUserFromHeaders(req);
if (session?.user?.email) {
userEmail = session.user.email;
logger.info(`Authenticated user: ${userEmail}`);
console.log(`Authenticated user from session: ${userEmail}`);
} else if (headerUser?.email) {
userEmail = headerUser.email;
console.log(`Authenticated user from headers: ${userEmail}`);
} else {
logger.warn('No authenticated session found, but continuing for demonstration');
console.warn('No authenticated session found, but continuing for demonstration');
}
} catch (authError) {
logger.error('Authentication error but continuing:', authError);
console.error('Authentication error but continuing:', authError);
}
// Parse request body
let data;
try {
data = await req.json();
logger.info('Request body:', data);
console.log('Request body:', data);
} catch (parseError) {
logger.error('Failed to parse request body:', parseError);
console.error('Failed to parse request body:', parseError);
return NextResponse.json({ error: 'Invalid request body' }, { status: 400 });
}
const { walletAddress, animalName } = data;
if (!walletAddress) {
logger.error('No wallet address provided for token award');
console.error('No wallet address provided for token award');
return NextResponse.json({ error: 'Wallet address is required' }, { status: 400 });
}
logger.info(`Processing token award for wallet ${walletAddress} and animal ${animalName || 'Unknown'}`);
console.log(`Processing token award for wallet ${walletAddress} and animal ${animalName || 'Unknown'}`);
// Check for environment variables
const apiUrl = process.env.NEXT_PUBLIC_TOKEN_BACKEND_URL;
const apiKey = process.env.NEXT_PUBLIC_TOKEN_API_KEY;
logger.info(`Using backend URL: ${apiUrl || 'Not set'}, API key exists: ${!!apiKey}`);
console.log(`Using backend URL: ${apiUrl || 'Not set'}, API key exists: ${!!apiKey}`);
const result = await awardTokensForSighting(walletAddress, userEmail, animalName || 'Unknown wildlife');
logger.info(`Token award result:`, result);
console.log(`Token award result:`, result);
// Return the complete result directly without nesting
return NextResponse.json(result);
} catch (error) {
logger.error('Error in award-tokens API:', error);
console.error('Error in award-tokens API:', error);
return NextResponse.json({
success: false,
error: error instanceof Error ? error.message : 'Failed to award tokens'

View File

@ -16,6 +16,8 @@ interface AnalysisResult {
error?: string;
isAnimal?: boolean;
tokenReward?: TokenReward | null;
animalType?: string;
pointsAwarded?: number;
}
interface ImageAnalysisCardProps {