express-recaptcha-middleware

1.0.1 • Public • Published

express-recaptcha-middleware

Validates Google ReCaptcha, and rejects the request on error.

Dependency
Usage
Errors
Full example

npm install --save express-recaptcha-middleware

Dependency

Requires body-parser.urlencoded() middleware.

Usage

var ReCaptchaMiddleware = require('express-recaptcha-middleware')('YOUR_GOOGLE_RECAPTCHA_SECRET');
 
express.post('/contact', ReCaptchaMiddleware('recaptcha-field-name'), routeHandler);

Errors

  • NotFoundReCaptcha ReCaptcha is missing from req.body
  • InvalidReCaptcha ReCaptcha has been rejected by Google's servers
    • prop errorCodes: contains any errorCode returned

Full Example with Express

var express = require('express')()
    , bodyParser = require('body-parser')
    , ReCaptchaMiddleware = require('express-recaptcha-middleware')('YOUR_GOOGLE_RECAPTCHA_SECRET');
 
express.use(bodyParser.urlencoded({extended: false}));
 
express.post('/new-post',
    ReCaptchaMiddleware('recaptcha-field-name'), // ReCaptcha field name defaults to `g-recaptcha-response`
    function (req, res, next) {
        /*
        * ReCaptcha has been validated
        */
    }
);
 
express.use(function (err, req, res, next) {
    if (err.name === 'NotFoundReCaptcha')
        // ReCaptcha is missing from req.body
    else if (err.name === 'InvalidReCaptcha')
        /* ReCaptcha has been rejected by Google's servers.
        *  Check err.errorCodes and https://developers.google.com/recaptcha/docs/verify#error-code-reference for more information */
});
 
express.listen(1337);

Package Sidebar

Install

npm i express-recaptcha-middleware

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • yachaka