11
22import { NextResponse } from "next/server" ;
33import { z } from "zod" ;
4- import { client } from "@/sanity/lib/client" ;
4+ import { apiVersion , dataset , projectId } from "@/sanity/lib/api" ;
5+ import { createClient } from "next-sanity" ;
6+
7+ const sanityWriteClient = createClient ( {
8+ projectId,
9+ dataset,
10+ apiVersion,
11+ token : process . env . SANITY_API_WRITE_TOKEN ,
12+ perspective : "published" ,
13+ useCdn : false ,
14+ } ) ;
515
616const formSchema = z . object ( {
717 fullName : z . string ( ) ,
@@ -10,6 +20,7 @@ const formSchema = z.object({
1020 sponsorshipTier : z . array ( z . string ( ) ) ,
1121 message : z . string ( ) . optional ( ) ,
1222 honeypot : z . string ( ) . optional ( ) ,
23+ "cf-turnstile-response" : z . string ( ) ,
1324} ) ;
1425
1526export async function POST ( request : Request ) {
@@ -23,34 +34,36 @@ export async function POST(request: Request) {
2334 sponsorshipTier,
2435 message,
2536 honeypot,
37+ "cf-turnstile-response" : turnstileToken ,
2638 } = formSchema . parse ( body ) ;
2739
2840 // Honeypot check
2941 if ( honeypot ) {
3042 return NextResponse . json ( { message : "Spam detected" } , { status : 400 } ) ;
3143 }
3244
33- // TODO: Verify Cloudflare Turnstile token
34- // const turnstileToken = request.headers.get("X-Turnstile-Token");
35- // const ip = request.headers.get("CF-Connecting-IP");
36- // const turnstileResponse = await fetch(
37- // "https://challenges.cloudflare.com/turnstile/v0/siteverify",
38- // {
39- // method: "POST",
40- // headers: {
41- // "Content-Type": "application/json",
42- // },
43- // body: JSON.stringify({
44- // secret: process.env.CLOUDFLARE_TURNSTILE_SECRET_KEY,
45- // response: turnstileToken,
46- // remoteip: ip,
47- // }),
48- // }
49- // );
50- // const turnstileData = await turnstileResponse.json();
51- // if (!turnstileData.success) {
52- // return NextResponse.json({ message: "Spam detected" }, { status: 400 });
53- // }
45+ const ip = request . headers . get ( "CF-Connecting-IP" ) ;
46+ const turnstileResponse = await fetch (
47+ "https://challenges.cloudflare.com/turnstile/v0/siteverify" ,
48+ {
49+ method : "POST" ,
50+ headers : {
51+ "Content-Type" : "application/json" ,
52+ } ,
53+ body : JSON . stringify ( {
54+ secret : process . env . CLOUDFLARE_TURNSTILE_SECRET_KEY ,
55+ response : turnstileToken ,
56+ remoteip : ip ,
57+ } ) ,
58+ }
59+ ) ;
60+ const turnstileData = await turnstileResponse . json ( ) ;
61+ if ( ! turnstileData . success ) {
62+ return NextResponse . json (
63+ { message : "Invalid CAPTCHA" , details : turnstileData [ "error-codes" ] } ,
64+ { status : 400 }
65+ ) ;
66+ }
5467
5568 const sponsorshipRequest = {
5669 _type : "sponsorshipRequest" ,
@@ -61,7 +74,14 @@ export async function POST(request: Request) {
6174 message,
6275 } ;
6376
64- await client . create ( sponsorshipRequest ) ;
77+ try {
78+ await sanityWriteClient . create ( sponsorshipRequest ) ;
79+ } catch ( error ) {
80+ return NextResponse . json (
81+ { message : "Failed to save sponsorship request" , details : error } ,
82+ { status : 500 }
83+ ) ;
84+ }
6585
6686 return NextResponse . json ( { message : "Sponsorship request submitted successfully" } ) ;
6787 } catch ( error ) {
0 commit comments