Overview
The Plugin Agency contact form uses Google reCAPTCHA v2 to prevent spam and automated bot submissions. This guide covers both frontend and backend integration.What is reCAPTCHA?
reCAPTCHA is a free service from Google that protects websites from spam and abuse by using advanced risk analysis to distinguish humans from bots.This project uses reCAPTCHA v2 with the checkbox challenge (“I’m not a robot”).
Site Key vs Secret Key
reCAPTCHA uses two keys:| Key | Location | Purpose |
|---|---|---|
| Site Key | Frontend (public) | Renders the reCAPTCHA widget in the browser |
| Secret Key | Backend (private) | Verifies the captcha token on the server |
Frontend Integration
Installation
The project uses thereact-google-recaptcha package:
Implementation
FromContact.jsx:
Key Features
- Lazy Loading: The reCAPTCHA component is lazy-loaded to improve performance
- Suspense Fallback: Shows loading state while reCAPTCHA loads
- Ref Management: Uses
useRefto programmatically reset the captcha - Token State: Stores the captcha token in React state
- Validation: Checks for token before allowing form submission
Error Boundary
The implementation includes an error boundary for graceful degradation:The error boundary prevents the entire form from crashing if reCAPTCHA fails to load.
Backend Verification
Verification Flow
Fromsend-email.js:
Verification Endpoint
Google’s verification endpoint:secret: Your secret key (from environment variable)response: The captcha token from the frontend
Error Handling
The backend handles three captcha-related error scenarios:| Error | Status Code | Response |
|---|---|---|
| Missing token | 400 | { "error": "Faltan campos obligatorios o el captcha" } |
| Invalid token | 400 | { "error": "Captcha inválido" } |
| Verification failure | 500 | { "error": "Error al verificar captcha" } |
Environment Setup
Frontend Environment Variable
Create a.env file in your project root:
The
VITE_ prefix is required for Vite to expose the variable to the frontend.Backend Environment Variable
Add to your backend.env file:
Obtaining Keys
- Go to Google reCAPTCHA Admin Console
- Click “Create” or ”+” to register a new site
- Choose reCAPTCHA v2 with “I’m not a robot” checkbox
- Add your domain(s)
- Copy the Site Key (for frontend) and Secret Key (for backend)
Development Mode
The contact form gracefully handles missing reCAPTCHA configuration in development:Testing
Test Keys
Google provides test keys that always pass or fail: Always Pass:- Site Key:
6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI - Secret Key:
6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe
Testing Flow
- Set test keys in your environment variables
- Submit the contact form
- Verify the token is sent in the request
- Check backend logs for verification success
- Confirm email is sent
Common Issues
| Issue | Cause | Solution |
|---|---|---|
| Widget not appearing | Missing or invalid site key | Check VITE_RECAPTCHA_SITE_KEY |
| ”Captcha inválido” error | Invalid secret key | Verify RECAPTCHA_SECRET_KEY |
| Token not sent | Missing captchaToken in request | Ensure captchaToken state is populated |
| Localhost testing fails | Domain not registered | Add localhost in reCAPTCHA admin console |
Token Lifecycle
- User loads form: reCAPTCHA widget is rendered
- User checks box: Google validates and returns a token
- Token stored:
onChangecallback stores token in React state - Form submission: Token is sent with form data to backend
- Backend verification: Token is verified with Google’s API
- Token reset: After successful submission, widget is reset via
captchaRef.current.reset()
Tokens expire after 2 minutes. If a user waits too long after completing the captcha, they may need to verify again.
Security Best Practices
- Never commit keys: Add
.envto.gitignore - Use environment variables: Keep keys out of source code
- Verify on backend: Always verify tokens server-side, never trust client
- Reset after use: Clear tokens after submission
- Handle errors gracefully: Provide clear feedback when verification fails
- Domain restrictions: Register only your production domains
Related
- Send Email API - API endpoint that uses reCAPTCHA verification
- Google reCAPTCHA Documentation