Giter Site home page Giter Site logo

ronaldlong46 / recaptcha2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fereidani/recaptcha2

0.0 1.0 0.0 28 KB

Easy verifier for google reCAPTCHA version 2 for Node.js and Express.js

License: MIT License

CoffeeScript 73.16% JavaScript 26.84%

recaptcha2's Introduction

reCAPTCHA2

Easy verifier for Google reCAPTCHA version 2 for Node.js

How to use

Step 1: Setup reCAPTCHA on your site

You need to receive your site key and secret key for your domain from https://www.google.com/recaptcha/intro/.

Follow the steps on this page to include the reCAPTCHA on your website.

Step 2: Initialize verifier

const reCAPTCHA = require('recaptcha2');

const recaptcha = new reCAPTCHA({
  siteKey: 'your-site-key', // retrieved during setup
  secretKey: 'your-secret-key', // retrieved during setup
  ssl: false // optional, defaults to true.
             // Disable if you don't want to access
             // the Google API via a secure connection
});

Step 3: Verifying the reCAPTCHA response

reCAPTCHA2 uses promises to validate the reCAPTCHA response, you can use one of the following methods:

  • please mention on catch, library passes error codes from google which you can translate with translateErrors method

Simple usage:

async reCaptcha(key){
  try {
    const validation = await recaptcha.validate(key);
    // returns type boolean true if validation is successful
    return validation;
  } catch (error) {
    // invalid
    console.log(recaptcha.translateErrors(errorCodes)); // translate error codes to human readable text
    return error;
  }
}

Optional: You can also pass the clients IP address to the validate method after the key. For more information on that, please see the reCAPTCHA documentation.

For use with Express (you need body-parser):

function submitForm(req, res) {
  recaptcha.validateRequest(req)
    .then(function(){
      // validated and secure
      res.json({formSubmit:true})
    })
    .catch(function(errorCodes){
      // invalid
      res.json({
        formSubmit: false,
        errors: recaptcha.translateErrors(errorCodes) // translate error codes to human readable text
      });
    });
}

Generating the reCAPTCHA widget

recaptcha.formElement() returns standard form element for reCAPTCHA which you should include at the end of your html form element.

You can also set CSS classes like this: recaptcha.formElement('custom-class-for-recaptcha'). The default class is g-recaptcha.

<div class="custom-class-for-recaptcha" data-sitekey="your-site-key"></div>

Changelog

Please see the CHANGELOG.md.

recaptcha2's People

Contributors

fereidani avatar doomhz avatar fishcharlie avatar mixalistzikas avatar ronaldl29 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.