npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-recaptcha3

v1.4.0

Published

Angular service for google recaptcha3

Downloads

1,561

Readme

ng-recaptcha3

Angular service for Google reCAPTCHA 3

See Demo

Installation

Run following command to install ng-recaptcha3

npm i ng-recaptcha3 --save

How to use?

reCAPTCHA v3 introduces a new concept: actions. When you specify an action name in each place you execute reCAPTCHA you enable two new features:

  • a detailed break-down of data for your top ten actions in the admin console
  • adaptive risk analysis based on the context of the action (abusive behavior can vary) Importantly, when you verify the reCAPTCHA response you should also verify that the action name matches the one you expect.

Front

At first you need to import NgRecaptcha3Service to your component

import { NgRecaptcha3Service } from 'ng-recaptcha3'

Then inject it to your component constructor

export class AppComponent implements OnInit {


  constructor(private recaptcha3: NgRecaptcha3Service) {
  

  }

Pass your siteKey to init function

  ngOnInit() {    
    this.recaptcha3.init(YOUR_SITE_KEY)
  }

The init function will return Promise with status parameter that will indicate script loaded status

  ngOnInit() {    
    this.recaptcha3.init(YOUR_SITE_KEY).then(status => {
      // status: success/error
      // success - script is loaded and greaptcha is ready
      // error - script is not loaded
      console.log(status)
    })
  }

On form submit generate recaptcha token (it will be checked in backend) using siteKey

onSubmit() {
  this.submitted = true;
  if (this.myForm.inValid) {
    return;
  }

  // generate new token
  this.recaptcha3.getToken().then(token => {
    const formData = this.myForm.value;
    formData.recaptchaToken = token;
    // send data with token to backend
    this.http.post(url,formData) ....

  }, error => {
    // get error, e.g. if key is invalid
    console.log(error)
  }

}

Execute getToken with action name. See more here

this.recaptcha3.getToken({ action: 'homepage' })

You can destroy recaptcha in ngOnDestroy

public ngOnDestroy() {
  this.recaptcha3.destroy();
}

Backend

In backend we need to verify given token using secretKey.

node.js example

const request = require('request-promise');

const secretKey = YOUR_RECAPTCHA_SECRET_KEY;
const userIp = 'USER_IP';
    request.get(
      {
          url: `https://www.google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${recaptchaToken}&remoteip=${userIp}`,
      }).then((response) => {

      // If response false return error message
      if (response.success === false) {
          return res.json({success: false, error: 'Recaptcha token validation failed'});
      }
      // otherwise continue handling/saving form data
      next();
  })

PHP example

$recaptchaToken = isset($_POST['recaptchaToken']) ? $_POST['recaptchaToken'] : false;

  if(!$recaptchaToken) {
    //Do something with error
  }
  
  $secretKey = YOUR_RECAPTCHA_SECRET_KEY;
  $userIp = $_SERVER['REMOTE_ADDR'];
  $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$recaptchaToken."&remoteip=".$userIp);
  
  if($response.success == false){
              //Do something with error
              
  } else {
    // reCaptchaToken is valid you can continue with the rest of your code
  }