{"id":15062089,"url":"https://github.com/pdupavillon/express-recaptcha","last_synced_at":"2025-04-05T03:11:08.239Z","repository":{"id":24422027,"uuid":"27823094","full_name":"pdupavillon/express-recaptcha","owner":"pdupavillon","description":"Implementation of google recaptcha v2 \u0026 V3 solutions for express.js","archived":false,"fork":false,"pushed_at":"2022-12-10T16:11:41.000Z","size":542,"stargazers_count":129,"open_issues_count":13,"forks_count":21,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T02:07:02.319Z","etag":null,"topics":["captcha","express-recaptcha","expressjs","google","google-recaptcha","javascript","middleware","nodejs","recaptcha","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pdupavillon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-10T14:20:29.000Z","updated_at":"2025-01-13T12:32:41.000Z","dependencies_parsed_at":"2023-01-14T00:56:43.220Z","dependency_job_id":null,"html_url":"https://github.com/pdupavillon/express-recaptcha","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdupavillon%2Fexpress-recaptcha","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdupavillon%2Fexpress-recaptcha/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdupavillon%2Fexpress-recaptcha/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdupavillon%2Fexpress-recaptcha/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pdupavillon","download_url":"https://codeload.github.com/pdupavillon/express-recaptcha/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247280272,"owners_count":20912967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["captcha","express-recaptcha","expressjs","google","google-recaptcha","javascript","middleware","nodejs","recaptcha","typescript"],"created_at":"2024-09-24T23:30:11.458Z","updated_at":"2025-04-05T03:11:08.212Z","avatar_url":"https://github.com/pdupavillon.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# express-recaptcha\n\n[![NPM](https://nodei.co/npm/express-recaptcha.png?compact=true)](https://nodei.co/npm/express-recaptcha/)\n\n[![Build Status][ci-image]][ci-url]\n[![npm version][npm-version-image]][npm-version-url]\n\n[Google recaptcha][google-recaptcha] middleware for express.\n\n[express-recaptcha v2][express-recaptcha-v2] (previous middleware version).\n\n## Table of contents\n\n-   [Installation](#installation)\n-   [Requirements](#requirements)\n-   [Usage](#usage)\n    -   [How to initialise](#how-to-initialise)\n    -   [`options` available/properties](#options-availableproperties)\n    -   [Render - `recaptcha.middleware.render`](#render---recaptchamiddlewarerender)\n    -   [Render and override options - `recaptcha.middleware.renderWith`](#render---recaptchamiddlewarerenderwith)\n    -   [Verify - `recaptcha.middleware.verify`](#verify---recaptchamiddlewareverify)\n    -   [List of possible error codes](#list-of-possible-error-codes)\n-   [Examples](#examples)\n\n## Installation\n\n```shell\nnpm install express-recaptcha --save\n```\n\n## Requirements\n\n-   [Expressjs][expressjs]\n-   A [body parser][body-parser] middleware to get captcha data from query: (If you're using an express version older than 4.16.0)\n    ```javascript\n    app.use(bodyParser.json())\n    app.use(bodyParser.urlencoded({ extended: true }))\n    ```\n\n## Usage\n\n### How to initialise:\n\n```javascript\nvar Recaptcha = require('express-recaptcha').RecaptchaV3\n//import Recaptcha from 'express-recaptcha'\nvar recaptcha = new Recaptcha('SITE_KEY', 'SECRET_KEY')\n//or with options\nvar options = { hl: 'de' }\nvar recaptcha = new Recaptcha('SITE_KEY', 'SECRET_KEY', options)\n```\n\n#### `options` available/properties:\n\n| option               | description                                                                                                                                                                                                                   |\n| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `onload`             | The callback function that gets called when all the dependencies have loaded.                                                                                                                                                 |\n| `hl`                 | Forces the widget to render in a specific language (Auto-detects if unspecified).                                                                                                                                             |\n| `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)                                            |\n| `action`             | **homepage** by default should only be alphanumeric [More info on google's web site](Google-recaptcha-action)                                                                                                                 |\n| `checkremoteip`      | Adding support of remoteip verification (based on x-forwarded-for header or remoteAddress.Value could be **true** OR **false** (default **false**).                                                                           |\n| `useRecaptchaDomain` | Boolean. Sets `www.recaptcha.net` as the host; useful in instances where `www.google.com` may be blocked (as detailed in the [reCaptcha docs](https://developers.google.com/recaptcha/docs/faq#can-i-use-recaptcha-globally)) |\n\n**For more information, please refer to:**\n\n-   [reCaptcha - display](https://developers.google.com/recaptcha/docs/display#config)\n-   [reCaptcha - verify ](https://developers.google.com/recaptcha/docs/verify)\n\n### Render - `recaptcha.middleware.render`\n\nThe 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:\n\n```javascript\napp.get('/', recaptcha.middleware.render, function (req, res) {\n    res.render('login', { captcha: res.recaptcha })\n})\n```\n\n### Render - `recaptcha.middleware.renderWith`\n\nSame as the render middleware method except that you can override the options in parameter :\n\n```javascript\napp.get(\n    '/',\n    recaptcha.middleware.renderWith({ hl: 'fr' }),\n    function (req, res) {\n        res.render('login', { captcha: res.recaptcha })\n    }\n)\n```\n\n### Verify - `recaptcha.middleware.verify`\n\nThe middleware's verify method sets the `recaptcha` property of `req` object, with validation information:\n\n```javascript\napp.post('/', recaptcha.middleware.verify, function (req, res) {\n    if (!req.recaptcha.error) {\n        // success code\n    } else {\n        // error code\n    }\n})\n```\n\nThe response verification is performed on `params`, `query`, and `body` properties for the `req` object.\n\nHere is an example of a `req.recaptcha` response:\n\n#### Example of verification response:\n\n```javascript\n{\n  error: string, // error code (see table below), null if success\n  data: {\n    hostname: string, // the site's hostname where the reCAPTCHA was solved\n    score: number, // the score for this request (0.0 - 1.0)\n    action: string // the action name for this request (important to verify)\n  }\n}\n```\n\n#### List of possible error codes:\n\n| Error code               | Description                                     |\n| :----------------------- | :---------------------------------------------- |\n| `missing-input-secret`   | The secret parameter is missing.                |\n| `invalid-input-secret`   | The secret parameter is invalid or malformed.   |\n| `missing-input-response` | The response parameter is missing.              |\n| `invalid-input-response` | The response parameter is invalid or malformed. |\n| `invalid-json-response`  | Can't parse google's response. Server error.    |\n\n## Examples\n\n### express-recaptcha - with verification middleware:\n\n```javascript\nvar express = require('express')\nvar bodyParser = require('body-parser')\nvar pub = __dirname + '/public'\nvar app = express()\nvar Recaptcha = require('express-recaptcha').RecaptchaV3\n\nvar recaptcha = new Recaptcha('SITE_KEY', 'SECRET_KEY', { callback: 'cb' })\n\n//- required by express-recaptcha in order to get data from body or query.\napp.use(bodyParser.json())\napp.use(bodyParser.urlencoded())\n\napp.use(express.static(pub))\napp.set('views', __dirname + '/views')\napp.set('view engine', 'jade')\n\napp.get('/', recaptcha.middleware.render, function (req, res) {\n    res.render('login', { captcha: res.recaptcha })\n})\n\n// override default options for that route\napp.get(\n    '/fr',\n    recaptcha.middleware.renderWith({ hl: 'fr' }),\n    function (req, res) {\n        res.render('login', { captcha: res.recaptcha })\n    }\n)\n\napp.post('/', recaptcha.middleware.verify, function (req, res) {\n    if (!req.recaptcha.error) {\n        // success code\n    } else {\n        // error code\n    }\n})\n```\n\n### express-recaptcha - without verification middleware: (using `recaptcha.verify` callback instead)\n\n```javascript\nvar express = require('express')\nvar bodyParser = require('body-parser')\nvar pub = __dirname + '/public'\nvar app = express()\nvar Recaptcha = require('express-recaptcha').RecaptchaV3\n\nvar recaptcha = new Recaptcha('SITE_KEY', 'SECRET_KEY', { callback: 'cb' })\n\n//- required by express-recaptcha in order to get data from body or query.\napp.use(bodyParser.json())\napp.use(bodyParser.urlencoded())\n\napp.use(express.static(pub))\napp.set('views', __dirname + '/views')\napp.set('view engine', 'jade')\n\napp.get('/', function (req, res) {\n    res.render('login', { captcha: recaptcha.render() })\n})\n\n// override default options for that route\napp.get('/fr', function (req, res) {\n    res.render('login', { captcha: recaptcha.renderWith({ hl: 'fr' }) })\n})\n\napp.post('/', function (req, res) {\n    recaptcha.verify(req, function (error, data) {\n        if (!error) {\n            // success code\n        } else {\n            // error code\n        }\n    })\n})\n```\n\n### Demo:\n\nRun the example folder for a live demo:\n\n```\n$ node example\\server.js\n```\n\n[ci-image]: https://github.com/pdupavillon/express-recaptcha/actions/workflows/ci.yml/badge.svg\n[ci-url]: https://github.com/pdupavillon/express-recaptcha/actions/workflows/ci.yml\n[npm-version-image]: https://badge.fury.io/js/express-recaptcha.svg\n[npm-version-url]: https://badge.fury.io/js/express-recaptcha\n[expressjs]: https://github.com/expressjs/express\n[body-parser]: https://github.com/expressjs/body-parser\n[google-recaptcha]: https://www.google.com/recaptcha\n[express-recaptcha-v2]: README.v2.md\n[google-recaptcha-action]: https://developers.google.com/recaptcha/docs/v3#actions\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdupavillon%2Fexpress-recaptcha","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpdupavillon%2Fexpress-recaptcha","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdupavillon%2Fexpress-recaptcha/lists"}