Files
ayris-legal-main/app/api/contact/route.ts
T
2026-08-09 11:20:13 +03:00

83 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextRequest, NextResponse } from 'next/server'
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
// Basit in-memory rate limiting (MVP — production'da Redis kullan)
const ipRequestMap = new Map<string, { count: number; resetAt: number }>()
const RATE_LIMIT = 5 // maksimum istek
const WINDOW_MS = 60_000 // 1 dakika
function checkRateLimit(ip: string): boolean {
const now = Date.now()
const entry = ipRequestMap.get(ip)
if (!entry || now > entry.resetAt) {
ipRequestMap.set(ip, { count: 1, resetAt: now + WINDOW_MS })
return true
}
if (entry.count >= RATE_LIMIT) return false
entry.count++
return true
}
export async function POST(request: NextRequest) {
try {
// IP tespiti
const forwarded = request.headers.get('x-forwarded-for')
const ip = forwarded ? forwarded.split(',')[0].trim() : 'unknown'
// Rate limit
if (!checkRateLimit(ip)) {
return NextResponse.json(
{ success: false, error: 'Çok fazla istek. Lütfen bir dakika bekleyip tekrar deneyin.' },
{ status: 429 }
)
}
const body = await request.json()
const { name, email, barNo, message, _hp } = body
// Honeypot kontrolü
if (_hp) {
// Bot — 200 döndür ama işleme
return NextResponse.json({ success: true })
}
// Zorunlu alan doğrulaması
if (!name || typeof name !== 'string' || name.trim().length < 2) {
return NextResponse.json(
{ success: false, error: 'Ad Soyad zorunludur (en az 2 karakter).' },
{ status: 400 }
)
}
if (!email || typeof email !== 'string' || !EMAIL_REGEX.test(email.trim())) {
return NextResponse.json(
{ success: false, error: 'Geçerli bir e-posta adresi giriniz.' },
{ status: 400 }
)
}
// MVP: Konsola yaz
// TODO: Resend/SendGrid veya Prisma leads tablosu eklenebilir
console.log('[AyrisLegal] 🎯 Yeni Demo Talebi:', {
timestamp: new Date().toISOString(),
name: name.trim(),
email: email.trim().toLowerCase(),
barNo: barNo?.trim() || null,
message: message?.trim() || null,
ip,
})
return NextResponse.json({ success: true }, { status: 200 })
} catch (err) {
console.error('[AyrisLegal] Contact API hatası:', err)
return NextResponse.json(
{ success: false, error: 'Sunucu hatası oluştu.' },
{ status: 500 }
)
}
}