Google reCAPTCHA helps protect websites from bots and automated abuse. Google reCAPTCHA v3 helps you protect your application from bots by running in the background without interrupting user experience.
This guide walks you through integrating reCAPTCHA with Next.js 14.
Import the useGoogleReCaptcha hook from "react-google-recaptcha-v3".
In this setup, useGoogleReCaptcha provides the executeRecaptcha function that triggers reCAPTCHA v3 in the background and returns the token upon success.
I prefered to use the a custom hook for verification:
You can declare the custom types for error and response as per listed in the official site. (You can read more from Google reCAPTCHA)
You can also construct the object to map the actial meaning of the these error codes:
Next, import the GoogleReCaptchaProvider from react-google-recaptcha-v3 and wrap your component to automatically load reCAPTCHA.
The GoogleReCaptchaProvider ensures that reCAPTCHA is loaded for your application.
Here, I created new component and exported the Provider.
Note pass your "reCaptcha site Key" as prop reCaptchaKey to the GoogleReCaptchaProvider:
Later you can import custom hook useReCaptcha use it as follow:
I also created a layout file as in my scenario you can use Provider component where it is necessary:
This type of approach is completly depends on developers and thier way coding styles.
Caution: This is how I implimented in my project there are other method to integrate in nextJs project either by using server actions or by injecting the script to the page
Google reCAPTCHA v3 offers a seamless, non-intrusive way to protect your Next.js application from bots. By using the react-google-recaptcha-v3 package, you can easily integrate this service into your forms.
Make sure to test both client-side and server-side logic to ensure your implementation is secure and functional.