{"id":16687361,"url":"https://github.com/rodrigokamada/angular-recaptcha-v3","last_synced_at":"2025-03-21T18:33:23.138Z","repository":{"id":38445254,"uuid":"399214266","full_name":"rodrigokamada/angular-recaptcha-v3","owner":"rodrigokamada","description":"Application example built with Angular 18 and adding the Google reCAPTCHA v3 using the ng-recaptcha library.","archived":false,"fork":false,"pushed_at":"2024-09-25T10:28:41.000Z","size":4944,"stargazers_count":8,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-19T03:28:44.098Z","etag":null,"topics":["angular","beginners","dev-community","frontend","gh-actions","gh-pages","hacktoberfest","recaptcha","stackblitz","tutorial","web"],"latest_commit_sha":null,"homepage":"https://rodrigokamada.github.io/angular-recaptcha-v3/","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/rodrigokamada.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"rodrigokamada"}},"created_at":"2021-08-23T18:45:35.000Z","updated_at":"2024-09-25T10:28:44.000Z","dependencies_parsed_at":"2024-05-28T15:06:04.282Z","dependency_job_id":null,"html_url":"https://github.com/rodrigokamada/angular-recaptcha-v3","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodrigokamada%2Fangular-recaptcha-v3","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodrigokamada%2Fangular-recaptcha-v3/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodrigokamada%2Fangular-recaptcha-v3/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodrigokamada%2Fangular-recaptcha-v3/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rodrigokamada","download_url":"https://codeload.github.com/rodrigokamada/angular-recaptcha-v3/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244849535,"owners_count":20520733,"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":["angular","beginners","dev-community","frontend","gh-actions","gh-pages","hacktoberfest","recaptcha","stackblitz","tutorial","web"],"created_at":"2024-10-12T15:08:55.893Z","updated_at":"2025-03-21T18:33:22.791Z","avatar_url":"https://github.com/rodrigokamada.png","language":"TypeScript","funding_links":["https://github.com/sponsors/rodrigokamada"],"categories":[],"sub_categories":[],"readme":"# Angular Google reCAPTCHA v3\n\n\nApplication example built with [Angular](https://angular.io/) 18 and adding the Google reCAPTCHA v3 using the [ng-recaptcha](https://www.npmjs.com/package/ng-recaptcha) library.\n\nThis tutorial was posted on my [blog](https://rodrigo.kamada.com.br/blog/adicionando-o-google-recaptcha-v3-em-uma-aplicacao-angular) in portuguese and on the [DEV Community](https://dev.to/rodrigokamada/adding-the-google-recaptcha-v3-to-an-angular-application-kge) in english.\n\n\n\n[![Website](https://shields.braskam.com/v1/shields?name=website\u0026format=rectangle\u0026size=small\u0026radius=5)](https://rodrigo.kamada.com.br)\n[![LinkedIn](https://shields.braskam.com/v1/shields?name=linkedin\u0026format=rectangle\u0026size=small\u0026radius=5)](https://www.linkedin.com/in/rodrigokamada)\n[![Twitter](https://shields.braskam.com/v1/shields?name=twitter\u0026format=rectangle\u0026size=small\u0026radius=5\u0026socialAccount=rodrigokamada)](https://twitter.com/rodrigokamada)\n[![Instagram](https://shields.braskam.com/v1/shields?name=instagram\u0026format=rectangle\u0026size=small\u0026radius=5)](https://www.instagram.com/rodrigokamada)\n\n\n\n## Prerequisites\n\n\nBefore you start, you need to install and configure the tools:\n\n* [git](https://git-scm.com/)\n* [Node.js and npm](https://nodejs.org/)\n* [Angular CLI](https://angular.io/cli)\n* IDE (e.g. [Visual Studio Code](https://code.visualstudio.com/))\n\n\n\n## Getting started\n\n\n### Create and configure the account on the Google reCAPTCHA\n\n\n**1.** Let's create the account. Access the site [https://www.google.com/recaptcha/](https://www.google.com/recaptcha/) and click on the button *v3 Admin Console*.\n\n![Google reCAPTCHA - Home page](https://res.cloudinary.com/rodrigokamada/image/upload/v1633966510/Blog/angular-recaptcha-v3/recaptcha-step1.png)\n\n**2.** Fill in the field *Email or phone* and click on the button *Next* to login with your Google account and if you don't have an account, just create a new account.\n\n![Google reCAPTCHA - Sign up](https://res.cloudinary.com/rodrigokamada/image/upload/v1633966510/Blog/angular-recaptcha-v3/recaptcha-step2.png)\n\n**3.** Click on the button *+*.\n\n![Google reCAPTCHA - Create new](https://res.cloudinary.com/rodrigokamada/image/upload/v1633966510/Blog/angular-recaptcha-v3/recaptcha-step3.png)\n\n**4.** Fill in the field *Label*, click on the option *reCAPTCHA 3*, Fill in the field *Domains*, click on the checkbox *Accept the reCAPTCHA Terms of Service* and click on the button *Submit*.\n\n![Google reCAPTCHA - Register a new site](https://res.cloudinary.com/rodrigokamada/image/upload/v1633966510/Blog/angular-recaptcha-v3/recaptcha-step4.png)\n\n**5.** Click on the button *COPY SITE KEY* to copy the key, in my case, the key `6Lf7UL0cAAAAAIt_m-d24WG4mA1XFPHE8yVckc5S` was copied because this key will be configured in the Angular application.\n\n![Google reCAPTCHA - Adding reCAPTCHA to your site](https://res.cloudinary.com/rodrigokamada/image/upload/v1633966510/Blog/angular-recaptcha-v3/recaptcha-step5.png)\n\n**6.** Ready! The keys have been generated.\n\n\n### Create the Angular application\n\n\n**1.** Let's create the application with the Angular base structure using the `@angular/cli` with the route file and the SCSS style format.\n\n```powershell\nng new angular-recaptcha-v3 --ssr false --routing true --style scss\nCREATE angular-recaptcha-v3/README.md (1082 bytes)\nCREATE angular-recaptcha-v3/.editorconfig (274 bytes)\nCREATE angular-recaptcha-v3/.gitignore (587 bytes)\nCREATE angular-recaptcha-v3/angular.json (2836 bytes)\nCREATE angular-recaptcha-v3/package.json (1055 bytes)\nCREATE angular-recaptcha-v3/tsconfig.json (860 bytes)\nCREATE angular-recaptcha-v3/tsconfig.app.json (263 bytes)\nCREATE angular-recaptcha-v3/tsconfig.spec.json (273 bytes)\nCREATE angular-recaptcha-v3/.vscode/extensions.json (130 bytes)\nCREATE angular-recaptcha-v3/.vscode/launch.json (470 bytes)\nCREATE angular-recaptcha-v3/.vscode/tasks.json (938 bytes)\nCREATE angular-recaptcha-v3/src/main.ts (250 bytes)\nCREATE angular-recaptcha-v3/src/index.html (307 bytes)\nCREATE angular-recaptcha-v3/src/styles.scss (80 bytes)\nCREATE angular-recaptcha-v3/src/app/app.component.scss (0 bytes)\nCREATE angular-recaptcha-v3/src/app/app.component.html (19903 bytes)\nCREATE angular-recaptcha-v3/src/app/app.component.spec.ts (970 bytes)\nCREATE angular-recaptcha-v3/src/app/app.component.ts (321 bytes)\nCREATE angular-recaptcha-v3/src/app/app.config.ts (310 bytes)\nCREATE angular-recaptcha-v3/src/app/app.routes.ts (77 bytes)\nCREATE angular-recaptcha-v3/public/favicon.ico (15086 bytes)\n✔ Packages installed successfully.\n    Successfully initialized git.\n```\n\n**2.** Install and configure the Bootstrap CSS framework. Do steps 2 and 3 of the post *[Adding the Bootstrap CSS framework to an Angular application](https://github.com/rodrigokamada/angular-bootstrap)*.\n\n**3.** Configure the `siteKey` variable with the Google reCAPTCHA key in the `src/environments/environment.ts` and `src/environments/environment.prod.ts` files as below.\n\n```typescript\nrecaptcha: {\n  siteKey: '6Lf7UL0cAAAAAIt_m-d24WG4mA1XFPHE8yVckc5S',\n},\n```\n\n**4.** Install the `ng-recaptcha` library.\n\n```powershell\nnpm install ng-recaptcha\n```\n\n**5.** Import the `FormsModule`, `RecaptchaV3Module` modules. Configure the Google reCAPTCHA key. Change the `app.module.ts` file and add the lines as below.\n\n```typescript\nimport { FormsModule } from '@angular/forms';\nimport { RECAPTCHA_V3_SITE_KEY, RecaptchaV3Module } from 'ng-recaptcha';\n\nimport { environment } from '../environments/environment';\n\nimports: [\n  BrowserModule,\n  FormsModule,\n  RecaptchaV3Module,\n  AppRoutingModule,\n],\nproviders: [\n  {\n    provide: RECAPTCHA_V3_SITE_KEY,\n    useValue: environment.recaptcha.siteKey,\n  },\n],\n```\n\n**6.** Remove the contents of the `AppComponent` class from the `src/app/app.component.ts` file. Import the `NgForm` component, the `ReCaptchaV3Service` service and create the `send` method as below.\n\n```typescript\nimport { Component } from '@angular/core';\nimport { NgForm } from '@angular/forms';\nimport { ReCaptchaV3Service } from 'ng-recaptcha';\n\n@Component({\n  selector: 'app-root',\n  templateUrl: './app.component.html',\n  styleUrls: ['./app.component.scss'],\n})\nexport class AppComponent {\n\n  constructor(private recaptchaV3Service: ReCaptchaV3Service) {\n  }\n\n  public send(form: NgForm): void {\n    if (form.invalid) {\n      for (const control of Object.keys(form.controls)) {\n        form.controls[control].markAsTouched();\n      }\n      return;\n    }\n\n    this.recaptchaV3Service.execute('importantAction')\n    .subscribe((token: string) =\u003e {\n      console.debug(`Token [${token}] generated`);\n    });\n  }\n\n}\n```\n\n**7.** Remove the contents of the `src/app/app.component.html` file. Add the `re-captcha` component as below.\n\n```html\n\u003cdiv class=\"container-fluid py-3\"\u003e\n  \u003ch1\u003eAngular reCAPTCHA v3\u003c/h1\u003e\n\n  \u003cform #form=\"ngForm\"\u003e\n    \u003cdiv class=\"row mt-3\"\u003e\n      \u003cdiv class=\"col-sm-12 mb-2\"\u003e\n        \u003cbutton type=\"button\" class=\"btn btn-sm btn-primary\" (click)=\"send(form)\"\u003eSend\u003c/button\u003e\n      \u003c/div\u003e\n    \u003c/div\u003e\n  \u003c/form\u003e\n\u003c/div\u003e\n```\n\n**8.** Run the application with the command below.\n\n```powershell\nnpm start\n\n\u003e angular-recaptcha-v3@1.0.0 start\n\u003e ng serve\n\n✔ Browser application bundle generation complete.\n\nInitial Chunk Files | Names         |      Size\nvendor.js           | vendor        |   2.75 MB\nstyles.css          | styles        | 266.71 kB\npolyfills.js        | polyfills     | 128.52 kB\nscripts.js          | scripts       |  76.33 kB\nmain.js             | main          |  12.28 kB\nruntime.js          | runtime       |   6.64 kB\n\n                    | Initial Total |   3.23 MB\n\nBuild at: 2021-10-09T22:00:31.213Z - Hash: f91dc9237b57212ebd83 - Time: 12001ms\n\n** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **\n\n\n✔ Compiled successfully.\n```\n\n**9.** Ready! Access the URL `http://localhost:4200/` and check if the application is working. See the application working on [GitHub Pages](https://rodrigokamada.github.io/angular-recaptcha-v3/) and [Stackblitz](https://stackblitz.com/edit/angular15-recaptcha-v3).\n\n![Angular Google reCAPTCHA v3](https://res.cloudinary.com/rodrigokamada/image/upload/v1633966502/Blog/angular-recaptcha-v3/angular-recaptcha-v3.png)\n\n\n\n## Cloning the application\n\n**1.** Clone the repository.\n\n```powershell\ngit clone git@github.com:rodrigokamada/angular-recaptcha-v3.git\n```\n\n**2.** Install the dependencies.\n\n```powershell\nnpm ci\n```\n\n**3.** Run the application.\n\n```powershell\nnpm start\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frodrigokamada%2Fangular-recaptcha-v3","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frodrigokamada%2Fangular-recaptcha-v3","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frodrigokamada%2Fangular-recaptcha-v3/lists"}