{"id":19623138,"url":"https://github.com/codeep/react-recaptcha-google","last_synced_at":"2025-04-28T04:31:40.877Z","repository":{"id":32437756,"uuid":"133500637","full_name":"codeep/react-recaptcha-google","owner":"codeep","description":"This library helps to integrate google recaptcha into your react project easily.","archived":false,"fork":false,"pushed_at":"2022-12-08T19:24:11.000Z","size":1693,"stargazers_count":27,"open_issues_count":26,"forks_count":20,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-19T06:57:56.468Z","etag":null,"topics":["grecaptcha","react","react-recaptcha","recaptcha"],"latest_commit_sha":null,"homepage":"https://developers.google.com/recaptcha/intro","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codeep.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-15T10:34:58.000Z","updated_at":"2024-02-29T00:16:20.000Z","dependencies_parsed_at":"2023-01-14T21:15:30.445Z","dependency_job_id":null,"html_url":"https://github.com/codeep/react-recaptcha-google","commit_stats":null,"previous_names":["codeep/react-recaptcha"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeep%2Freact-recaptcha-google","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeep%2Freact-recaptcha-google/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeep%2Freact-recaptcha-google/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeep%2Freact-recaptcha-google/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeep","download_url":"https://codeload.github.com/codeep/react-recaptcha-google/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251252133,"owners_count":21559733,"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":["grecaptcha","react","react-recaptcha","recaptcha"],"created_at":"2024-11-11T11:31:55.663Z","updated_at":"2025-04-28T04:31:39.462Z","avatar_url":"https://github.com/codeep.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-recaptcha-google\n\nThis component is created in order to make the experience of integrating Google ReCaptcha into React apps easier and smoother.\n\nCurrently, we are using ReCaptcha V2 here. ReCaptcha V3 is still in beta version; so, we will update our component when they release the stable version.\n\nP.S. It will open the ReCaptcha window only when there are some doubts by Google; otherwise, it will automatically generate the recaptcha token.\n\n## Installation\n\n`npm install react-recaptcha-google --save`\n\n## Usage\n\nThere are two components that you need to use.\n\n### 1. Use `loadReCaptcha()` to initialize the ReCaptcha\n\nThis function should be imported and called in the main (parent) component of your app. We recommend calling it in `componentDidMount()` of `App.js`.\n\n```\nimport { loadReCaptcha } from 'react-recaptcha-google'\n\n...\n\ncomponentDidMount() {\n  loadReCaptcha();\n}\n```\n\n\n### 2. Use `ReCaptcha` to integrate ReCaptcha in a particular component\n\n#### invisible Recaptcha\n\nCreate a new component with the following code and give it a try!\n\n```\nimport React, { Component } from 'react';\nimport { ReCaptcha } from 'react-recaptcha-google'\n\nclass ExampleComponent extends Component {\n  constructor(props, context) {\n    super(props, context);\n    this.onLoadRecaptcha = this.onLoadRecaptcha.bind(this);\n    this.verifyCallback = this.verifyCallback.bind(this);\n  }\n\n  componentDidMount() {\n    if (this.captchaDemo) {\n        console.log(\"started, just a second...\")\n        this.captchaDemo.reset();\n        this.captchaDemo.execute();\n    }\n  }\n\n  onLoadRecaptcha() {\n      if (this.captchaDemo) {\n          this.captchaDemo.reset();\n          this.captchaDemo.execute();\n      }\n  }\n\n  verifyCallback(recaptchaToken) {\n    // Here you will get the final recaptchaToken!!!  \n    console.log(recaptchaToken, \"\u003c= your recaptcha token\")\n  }\n\n  render() {\n    return (\n      \u003cdiv\u003e\n        {/* You can replace captchaDemo ref with whatever works for your component */}\n        \u003cReCaptcha\n            ref={(el) =\u003e {this.captchaDemo = el;}}\n            size=\"invisible\"\n            render=\"explicit\"\n            sitekey=\"your_site_key\"\n            onloadCallback={this.onLoadRecaptcha}\n            verifyCallback={this.verifyCallback}\n        /\u003e\n        \u003ccode\u003e\n          1. Add \u003cstrong\u003eyour site key\u003c/strong\u003e in the ReCaptcha component. \u003cbr/\u003e\n          2. Check \u003cstrong\u003econsole\u003c/strong\u003e to see the token.\n        \u003c/code\u003e\n      \u003c/div\u003e\n    );\n  };\n};\n\nexport default ExampleComponent;\n\n```\n\n\n#### Visible / Normal Recaptcha\n\nFor having a visible ReCaptcha, you should make two minor changes on the above-mentioned code.\n1. Replace the size prop value `invisible` (see the imported ReCaptcha component) with either `normal` or `compact`. Those will add a checkbox with 'I am not a robot' label.\n2. Remove `this.[captchaRef].execute()` lines from your code.\n\n##### Optional props\n\n* `data-theme` - you can add `theme` prop with a value of either `\"dark\"` or `\"light\"`(default) to control the background theme of the visible ReCaptcha (when size is `normal` or `compact`)\n* `data-badge` - you can send `badge` prop with one of the following values: bottomright (default), bottomleft, inline. This will allowyou to reposition the ReCaptcha badge.\n\n### 3. Save Google response into state or inside a hidden field\n\n```\n  verifyCallback(recaptchaToken) {\n    const {\n      change\n    } = this.props;\n    // inside hidden field\n    change('recaptchaResponse', recaptchaToken);\n    // or state\n    this.setState('recaptchaResponse', recaptchaToken)\n  }\n  \n  render() {\n  ...\n  return {\n     \u003cField\n       component={TextField}\n       name=\"recaptchaResponse\"\n       type=\"hidden\"\n       disabled/\u003e\n       ...\n       }\n```\n\n### 4. Implement code server side to validate the response\n\n```\nimport * as request from 'request'; // from \"web-request\": \"^1.0.7\",\n\nasync check(recaptchaResponse: string, remoteAddress: string): Promise\u003cboolean\u003e {\n const secretKey = \"\";\n    return new Promise\u003cboolean\u003e((resolve, reject) =\u003e {\n      const verificationUrl = 'https://www.google.com/recaptcha/api/siteverify?secret=' + secretKey + '\u0026response=' + recaptchaResponse + '\u0026remoteip=' + remoteAddress;\n      request(verificationUrl\n        , function(error, response, body) {\n          if (error) {\n            return reject(false);\n          }\n          if (response.statusCode !== 200) {\n            return reject(false);\n          }\n\n          body = JSON.parse(body);\n          const passCaptcha = !(body.success !== undefined \u0026\u0026 !body.success);\n          resolve(passCaptcha);\n        });\n    });\n  }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeep%2Freact-recaptcha-google","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeep%2Freact-recaptcha-google","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeep%2Freact-recaptcha-google/lists"}