All Projects → pdupavillon → Express Recaptcha

pdupavillon / Express Recaptcha

Licence: mit
Implementation of google recaptcha v2 & V3 solutions for express.js

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Express Recaptcha

Angular Recaptcha
AngularJS directive to add a reCaptcha widget to your form
Stars: ✭ 502 (+382.69%)
Mutual labels:  google, captcha, recaptcha
Method Override
Override HTTP verbs.
Stars: ✭ 548 (+426.92%)
Mutual labels:  middleware, expressjs
Recaptcha Spring Boot Starter
Spring Boot starter for Google's reCAPTCHA
Stars: ✭ 103 (-0.96%)
Mutual labels:  captcha, recaptcha
Recaptcha
reCAPTCHA = REcognize CAPTCHA: A Burp Suite Extender that recognize CAPTCHA and use for intruder payload 自动识别图形验证码并用于burp intruder爆破模块的插件
Stars: ✭ 596 (+473.08%)
Mutual labels:  captcha, recaptcha
Body Parser
Node.js body parsing middleware
Stars: ✭ 4,962 (+4671.15%)
Mutual labels:  middleware, expressjs
Swagger Express Middleware
Swagger 2.0 middlware and mocks for Express.js
Stars: ✭ 543 (+422.12%)
Mutual labels:  middleware, expressjs
Serve Favicon
favicon serving middleware
Stars: ✭ 586 (+463.46%)
Mutual labels:  middleware, expressjs
Serve Index
Serve directory listings
Stars: ✭ 360 (+246.15%)
Mutual labels:  middleware, expressjs
Session
Simple session middleware for Express
Stars: ✭ 5,571 (+5256.73%)
Mutual labels:  middleware, expressjs
Vhost
virtual domain hosting
Stars: ✭ 686 (+559.62%)
Mutual labels:  middleware, expressjs
Cookie Session
Simple cookie-based session middleware
Stars: ✭ 928 (+792.31%)
Mutual labels:  middleware, expressjs
Serve Static
Serve static files
Stars: ✭ 1,217 (+1070.19%)
Mutual labels:  middleware, expressjs
Iris
The fastest HTTP/2 Go Web Framework. AWS Lambda, gRPC, MVC, Unique Router, Websockets, Sessions, Test suite, Dependency Injection and more. A true successor of expressjs and laravel | 谢谢 https://github.com/kataras/iris/issues/1329 |
Stars: ✭ 21,587 (+20656.73%)
Mutual labels:  middleware, expressjs
Kraken Js
An express-based Node.js web application bootstrapping module.
Stars: ✭ 4,938 (+4648.08%)
Mutual labels:  middleware, expressjs
Express Openapi Validator
🦋 Auto-validates api requests, responses, and securities using ExpressJS and an OpenAPI 3.x specification
Stars: ✭ 436 (+319.23%)
Mutual labels:  middleware, expressjs
Cors
Node.js CORS middleware
Stars: ✭ 5,252 (+4950%)
Mutual labels:  middleware, expressjs
Express Status Monitor
🚀 Realtime Monitoring solution for Node.js/Express.js apps, inspired by status.github.com, sponsored by https://dynobase.dev
Stars: ✭ 3,302 (+3075%)
Mutual labels:  middleware, expressjs
Buster
Captcha solver extension for humans
Stars: ✭ 4,244 (+3980.77%)
Mutual labels:  captcha, recaptcha
Rucaptcha
Captcha gem for Rails Application. No dependencies. No ImageMagick, No RMagick.
Stars: ✭ 607 (+483.65%)
Mutual labels:  captcha, recaptcha
Beelabrecaptcha2bundle
💻 Symfony bundle for Google Recaptcha2
Stars: ✭ 47 (-54.81%)
Mutual labels:  captcha, recaptcha

express-recaptcha

NPM

Build Status npm version

Google recaptcha middleware for express.

express-recaptcha v2 (previous middleware version).

Table of contents

Installation

npm install express-recaptcha --save

Requirements

  • Expressjs
  • A body parser middleware to get captcha data from query: (If you're using an express version older than 4.16.0)
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    

Usage

How to initialise:

var Recaptcha = require('express-recaptcha').RecaptchaV3;
//import Recaptcha from 'express-recaptcha'
var recaptcha = new Recaptcha('SITE_KEY', 'SECRET_KEY');
//or with options
var options = {'hl':'de'};
var recaptcha = new Recaptcha('SITE_KEY', 'SECRET_KEY', options);

options available/properties:

option description
onload The callback function that gets called when all the dependencies have loaded.
hl Forces the widget to render in a specific language (Auto-detects if unspecified).
callback In that callback you will call your backend to verify the given token. To be verified, the token needs to be posted with the key g-recaptcha-response (see the example folder)
action homepage by default should only be alphanumeric More info on google's web site
checkremoteip Adding support of remoteip verification (based on x-forwarded-for header or remoteAddress.Value could be true OR false (default false).

For more information, please refer to:

Render - recaptcha.middleware.render

The middleware's render method sets the recaptcha property of res object, with the generated html code. Therefore, you can easily append recaptcha into your templates by passing res.recaptcha to the view:

app.get('/', recaptcha.middleware.render, function(req, res){
  res.render('login', { captcha:res.recaptcha });
});

Render - recaptcha.middleware.renderWith

Same as the render middleware method except that you can override the options in parameter :

app.get('/', recaptcha.middleware.renderWith({'hl':'fr'}), function(req, res){
  res.render('login', { captcha:res.recaptcha });
});

Verify - recaptcha.middleware.verify

The middleware's verify method sets the recaptcha property of req object, with validation information:

app.post('/', recaptcha.middleware.verify, function(req, res){
  if (!req.recaptcha.error) {
    // success code
  } else {
    // error code
  }
});

The response verification is performed on params, query, and body properties for the req object.

Here is an example of a req.recaptcha response:

Example of verification response:

{
  error: string, // error code (see table below), null if success
  data: {
    hostname: string, // the site's hostname where the reCAPTCHA was solved
    score: number, // the score for this request (0.0 - 1.0)
    action: string // the action name for this request (important to verify)
  }
}

List of possible error codes:

Error code Description
missing-input-secret The secret parameter is missing.
invalid-input-secret The secret parameter is invalid or malformed.
missing-input-response The response parameter is missing.
invalid-input-response The response parameter is invalid or malformed.
invalid-json-response Can't parse google's response. Server error.

Examples

express-recaptcha - with verification middleware:

var express = require('express');
var bodyParser = require('body-parser');
var pub = __dirname + '/public';
var app = express();
var Recaptcha = require('express-recaptcha').RecaptchaV3;

var recaptcha = new Recaptcha('SITE_KEY', 'SECRET_KEY',{callback:'cb'});

//- required by express-recaptcha in order to get data from body or query.
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

app.use(express.static(pub));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.get('/', recaptcha.middleware.render, function(req, res){
  res.render('login', { captcha:res.recaptcha });
});

// override default options for that route
app.get('/fr', recaptcha.middleware.renderWith({'hl':'fr'}), function(req, res){
  res.render('login', { captcha:res.recaptcha });
});

app.post('/', recaptcha.middleware.verify, function(req, res){
  if (!req.recaptcha.error) {
    // success code
  } else {
    // error code
  }
});

express-recaptcha - without verification middleware: (using recaptcha.verify callback instead)

var express = require('express');
var bodyParser = require('body-parser');
var pub = __dirname + '/public';
var app = express();
var Recaptcha = require('express-recaptcha').RecaptchaV3;

var recaptcha = new Recaptcha('SITE_KEY', 'SECRET_KEY', {callback:'cb'});

//- required by express-recaptcha in order to get data from body or query.
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

app.use(express.static(pub));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.get('/', function(req, res){
  res.render('login', { captcha:recaptcha.render() });
});

// override default options for that route
app.get('/fr', function(req, res){
  res.render('login', { captcha:recaptcha.renderWith({'hl':'fr'}) });
});

app.post('/', function(req, res){
  recaptcha.verify(req, function(error, data){
    if (!req.recaptcha.error) {
      // success code
    } else {
      // error code
    }
  });
});

Demo:

Run the example folder for a live demo:

$ node example\server.js
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].