0.2.0 • Published 1 year ago

recaptcha-node v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

recaptcha-node

A Node.js library to verify reCAPTCHA v2/v3 response tokens received from a client.

GitHub | NPM

Install

npm install recaptcha-node

Usage

Importing

If you are using reCAPTCHA v2, import the RecaptchaV2 class.

const {RecaptchaV2} = require('recaptcha-node');

If you are using reCAPTCHA v3, import the RecaptchaV3 class.

const {RecaptchaV3} = require('recaptcha-node');

Verifying a Response Token

To verify a response token using either RecaptchaV2 or RecaptchaV3, call the verify method.

The verify method's signature on RecaptchaV2 and RecaptchaV3 is nearly identical, differing only in the return value. RecaptchaV2's verify method returns an instance of RecaptchaV2Result, and RecaptchaV3's verify method returns an instance of RecaptchaV3Result.

const {RecaptchaV2} = require('recaptcha-node');

const recaptchaV2 = new RecaptchaV2('secretKey');

recaptchaV2.verify('responseToken')
.then(result => {
  if (result.success) {
    // reCAPTCHA response was valid.
  }
  else {
    // reCAPTCHA response was invalid.
  }
})
.catch(error =>  {
  // Request failed.
});

Configuration

The RecaptchaV2 and RecaptchaV3 classes can be initialized with an options object as the second argument.

const {RecaptchaV3} = require('recaptcha-node');

const recaptchaV3 = new RecaptchaV3('secretKey', {
  hostname: 'google.com',
  port: 443,
  protocol: 'https',
  timeout: 30 * 1000,
});
NameDefaultDescription
agentundefined
hostnamegoogle.comHostname that requests are made to
port80 if protocol is 'http', 443 if protocol is 'https'Port that requests are made to
protocol'https''https' or 'http'Protocol that requests are made with
timeout30000Milliseconds before a request times out. Setting to 0 will prevent the request from ever timing out

Objects

RecaptchaV2Result

PropertyTypeOptionalDescription
successbooleanfalseWhether this request was a valid reCAPTCHA token for your site
challengeTimestampDatefalseTimestamp of the challenge load
apkPackageNamestringtrueThe package name of the app where the reCAPTCHA was solved
hostnamestringtrueThe hostname of the site where the reCAPTCHA was solved
errorCodesstring[]true

RecaptchaV3Result

PropertyTypeOptionalDescription
successbooleanfalseWhether this request was a valid reCAPTCHA token for your site
scorenumberfalseThe score for this request (0.0 - 1.0)
actionstringfalseThe action name for this request
challengeTimestampDatefalseTimestamp of the challenge load
hostnamestringfalseThe hostname of the site where the reCAPTCHA was solved
errorCodesstring[]true