This document explains the Google reCAPTCHA v3 implementation in this project.
Google reCAPTCHA v3 has been integrated to protect all forms in the application from spam submissions. It works invisibly in the background without requiring users to solve challenges.
- next-recaptcha-v3 (v1.5.3) - A Next.js-specific library for Google reCAPTCHA v3
All forms in the application are now protected:
- Dynamic Forms (form.tsx) - Forms built with Payload CMS form builder
- Contact Form (contact-form.tsx) - Static contact form
- Newsletter Form (newsletter-form.tsx) - Email subscription form
Add these to your .env file:
# Google reCAPTCHA v3
NEXT_PUBLIC_RECAPTCHA_SITE_KEY=6LeKS0YsAAAAAPSeyt2TyKzYEcupY4wprH9HrdAQ
RECAPTCHA_SECRET_KEY=your_secret_key_hereImportant: Replace your_secret_key_here with your actual reCAPTCHA secret key from the Google reCAPTCHA admin console.
- Go to Google reCAPTCHA Admin Console
- Select your site (or create a new one if you haven't already)
- Copy the Secret Key
- Update the
RECAPTCHA_SECRET_KEYin your.envfile
The site key is already configured: 6LeKS0YsAAAAAPSeyt2TyKzYEcupY4wprH9HrdAQ
Make sure this matches your Google reCAPTCHA admin console settings.
- ReCaptchaProvider wraps the entire app in layout.tsx
- When a user submits a form,
executeRecaptcha()is called with an action name - Google evaluates the interaction and returns a token
- The token is sent along with form data to the API
- API endpoints receive the reCAPTCHA token
- Server makes a request to Google's siteverify API
- Google returns a verification result with a score (0.0-1.0)
- If score is below 0.5, the submission is rejected as suspicious
- If verification passes, the form data is processed normally
- /api/submissions - Handles Payload CMS form submissions
- /api/mailchimp - Handles newsletter subscriptions
Currently set to 0.5 (configurable in API routes):
- 1.0 = Very likely a human
- 0.5 = Threshold (suspicious)
- 0.0 = Very likely a bot
You can adjust this threshold based on your needs:
- Higher threshold (0.7+): More strict, may block some legitimate users
- Lower threshold (0.3-): More lenient, may allow some bots
To test the implementation:
- Development: reCAPTCHA works on localhost automatically
- Production: Ensure your domain is registered in Google reCAPTCHA admin
- Monitoring: Check Google reCAPTCHA admin console for analytics
- reCAPTCHA v3 runs automatically when forms are submitted
- Check browser console for any reCAPTCHA errors
- View Network tab to confirm token is being sent
- Check API responses for verification results
-
"reCAPTCHA verification failed"
- Check if
RECAPTCHA_SECRET_KEYis correctly set - Verify the secret key matches your site key in Google admin
- Check if
-
"Suspicious activity detected"
- User's interaction score was below 0.5
- Consider adjusting the threshold or investigating the user's behavior
-
Token missing
- Ensure
ReCaptchaProviderwraps your app in layout.tsx - Check if
useReCaptchahook is properly imported in form components
- Ensure
-
Domain mismatch errors
- Add your domain to the allowed domains list in Google reCAPTCHA admin
- localhost is allowed by default for testing
- Never expose your secret key - Keep it in
.envfile (not committed to git) - Always verify on the server - Client-side verification can be bypassed
- Monitor scores - Regularly check Google admin console for patterns
- Adjust thresholds - Based on your specific use case and spam levels