Tom Butcher 6eecd3c414 Initial commit (by create-cloudflare CLI)
Details:
  C3 = create-cloudflare@2.39.0
  project name = web-tombutcher-work
  package manager = npm@10.8.3
  wrangler = wrangler@3.111.0
  git = 2.39.5 (Apple Git-154)
2025-03-03 21:59:21 +00:00

45 lines
1.3 KiB
JavaScript

import { getLocation } from '../utils/geolocation.js';
import { addToNotionDatabase } from '../utils/notion.js';
const TURNSTILE_SECRET_KEY = 'your-turnstile-secret-key';
export async function handleContactRequest(request) {
const { email, token } = await request.json();
if (!email || !token) {
return new Response('Email and token are required', { status: 400 });
}
// Extract the IP address from the request headers
const ip = request.headers.get('CF-Connecting-IP');
// Get location info
const location = await getLocation(ip);
// Verify the Turnstile token
const verificationResponse = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
secret: TURNSTILE_SECRET_KEY,
response: token,
}),
});
const verificationData = await verificationResponse.json();
if (!verificationData.success) {
return new Response('Turnstile verification failed', { status: 400 });
}
// Add the email and location to Notion
try {
await addToNotionDatabase(email, location);
return new Response(JSON.stringify({ success: true, message: 'Email processed and added to Notion' }), {
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
return new Response('Error processing email', { status: 500 });
}
}