Google reCAPTCHA Implementation in Next Js

  • To implement Google reCAPTCHA in a React.js application using the `react-google-recaptcha` package, you need to follow these steps:
Step 1: Get reCAPTCHA keys
  • First, you need to register your site with Google reCAPTCHA and get the site key and secret key. You can do this by visiting the Google reCAPTCHA website (https://www.google.com/recaptcha/about/) and signing up for an API key pair for your site.


    NEXT_PUBLIC_GOOGLE_RECAPTCHA_SITE_KEY="LfYD5cpAAAAAFGNeiu2cxxLzDpuFFVRKRC7Ssk66V"
    NEXT_PUBLIC_GOOGLE_RECAPTCHA_SECRET_KEY="LfYD5cpAAAAAGFGJkGjj2DEwHjckue-sZSEpfZma6"

Step 2: Install `react-google-recaptcha`
  • You'll need to install the `react-google-recaptcha` package. You can do this by running the following command in your project directory:

    npm install react-google-recaptcha


Step 3: Use `ReCAPTCHA` Component
  • In your React component where you want the reCAPTCHA to appear, import `ReCAPTCHA` from `react-google-recaptcha` and use it by passing your site key. Here's an example of how to do it:

    import React, { useState } from 'react';
    import ReCAPTCHA from 'react-google-recaptcha';

    function MyComponent() {
        const [recaptchaValue, setRecaptchaValue] = useState(null);

        const handleOnChange = (value) => {
            console.log("Captcha value:", value);
            // Store the reCAPTCHA value or send it to the server here
            setRecaptchaValue(value);
        };

        return (
            <div>
                <form>
                    {/* Other form inputs go here */}
                    <ReCAPTCHA
                        sitekey="YOUR_SITE_KEY_HERE"
                        onChange={handleOnChange}
                    />
                    <button type="submit">Submit</button>
                </form>
            </div>
        );
    }

    export default MyComponent;

  • Replace `"YOUR_SITE_KEY_HERE"` with your actual reCAPTCHA site key.
Step 4: Verifying the reCAPTCHA response on the server
  • After the form is submitted, you should verify the reCAPTCHA response on your server. You'll need to send the reCAPTCHA response token (received in `handleOnChange`) to your server and use the reCAPTCHA secret key to verify it with Google's verification endpoint.
Here's a basic example of how you might verify the token on a Node.js server:


    const fetch = require('node-fetch');

    const verifyCaptcha = async (token) => {
        const secretKey = 'YOUR_SECRET_KEY_HERE';
        const response = await fetch(`https://www.google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${token}`, {
            method: 'GET',
        });
        const data = await response.json();

        if (data.success) {
            console.log("Verification successful");
            // Proceed with your form submission or user authentication
        } else {
            console.log("Verification failed", data['error-codes']);
            // Handle verification failure
        }
    };

  • Remember to replace `"YOUR_SECRET_KEY_HERE"` with your actual reCAPTCHA secret key. 
  • This setup ensures your React application integrates Google reCAPTCHA for added security and spam protection.

No comments:

Post a Comment

Primitive Types in TypeScript

In TypeScript, primitive types are the most basic data types, and they are the building blocks for handling data. They correspond to simple ...