-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
66 lines (57 loc) · 2.13 KB
/
Copy pathroute.ts
File metadata and controls
66 lines (57 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { NextRequest, NextResponse } from "next/server"
import { verifyRecaptcha } from "@/lib/recaptcha"
import { getPayload } from "payload"
import config from "@payload-config"
export async function POST(request: NextRequest) {
try {
console.log("=== API ROUTE: /api/submissions ===")
const body = await request.json()
console.log("Request body received:", {
hasRecaptchaToken: !!body.recaptchaToken,
recaptchaTokenLength: body.recaptchaToken?.length,
recaptchaTokenPreview: body.recaptchaToken?.substring(0, 20),
hasForm: !!body.form,
formId: body.form,
hasData: !!body.data,
dataKeys: body.data ? Object.keys(body.data) : [],
})
const { recaptchaToken, form, data } = body
if (!recaptchaToken) {
console.error("ERROR: No recaptchaToken in request!")
return NextResponse.json({ errors: [{ message: "reCAPTCHA token is required" }] }, { status: 400 })
}
// Verify reCAPTCHA token
console.log("Verifying reCAPTCHA token...")
const recaptchaResult = await verifyRecaptcha(recaptchaToken)
console.log("reCAPTCHA verification result:", recaptchaResult)
if (!recaptchaResult.success) {
console.error("reCAPTCHA verification failed:", recaptchaResult.error)
return NextResponse.json(
{
errors: [{ message: recaptchaResult.error || "reCAPTCHA verification failed" }]
},
{ status: 400 }
)
}
console.log("reCAPTCHA verified successfully!")
// Submit to Payload CMS directly
const payload = await getPayload({ config })
console.log("Creating submission in Payload...")
const submission = await payload.create({
collection: "submissions",
data: {
form,
data,
},
overrideAccess: true, // Bypass access control since we already verified reCAPTCHA
})
console.log("Submission created successfully:", submission.id)
return NextResponse.json(submission, { status: 200 })
} catch (error) {
console.error("Submission error:", error)
return NextResponse.json(
{ errors: [{ message: "Internal server error" }] },
{ status: 500 }
)
}
}