Vue implementation example using Google Recaptcha verification

Vue implementation example using Google Recaptcha verification

In our recent project, we need to use Google robot verification. The most important thing for this is to have a VPN, and we also need a Google account applied for by the company (it is useless to apply for it yourself) for commercial use. Use this account to apply for a secret key, and then once you have the secret key, you can do it.
Some of the code is as follows:

1. First of all, it is the Google robot verification component that I encapsulated:

<template>
    <div ref="grecaptcha">
    </div>
    <!-- <div id="robot"></div> -->

</template>
<script src="http://www.recaptcha.net/recaptcha/api.js?οnlοad=ReCaptchaLoaded&render=explicit&hl=en" async defer></script>
<script>
export default {
  props: ["sitekey"], // The secret key to be transferred mounted() {
    window.ReCaptchaLoaded = this.loaded;
    var script = document.createElement("script");
    script.src =
      "https://recaptcha.net/recaptcha/api.js?οnlοad=ReCaptchaLoaded&render=explicit";
    document.head.appendChild(script);
  },
  methods: {
    loaded() {
       window.grecaptcha.render(this.$refs.grecaptcha, {
        sitekey: this.sitekey,
        /**
        * res returns the verification status of Google.
        * Return string if successful * No string is returned if failed * So the verification status is determined based on the string */
        callback: res => {// true - validation successful// false - validation failedres.length > 0 ? this.$emit("getValidateCode", false) : his.$emit("getValidateCode", true)
        }
      });
      // grecaptcha.render('robot', {
      // sitekey: this.sitekey,
      // /**
      // * res returns the verification status of Google.
      // * Return string if successful// * Do not return string if failed// * So judge the verification status based on the string// */
      // theme: 'light', // theme color, light and dark are available // size: 'normal', // size rule, normal and compact are available // callback: res => {
      // res.length > 0 ? this.$emit("getValidateCode", true) : this.$emit("getValidateCode", false)
      // // true - authentication succeeded // false - authentication failed // }
      // }); 
     
    }
  },
};
</script>
<style>

</style>

2. Use where needed:

<template>
  <div>
    <div class="home-content">
      <div class="home-content-img">
        <!-- <div class="home-content-imgtop"> -->
          <img src="../../assets/image/202010_JP NIGHT Store Promotional Materials.002.png" alt="">
          <img src="../../assets/image/202010_JP NIGHT Store Promotional Materials.003.png" alt="">
        <!-- </div> -->
        <!-- <div class="home-content-imgbottom"> -->
          <img src="../../assets/image/202010_JP NIGHT Store Promotional Materials.004.png" alt="">
          <img src="../../assets/image/202010_JP NIGHT Store Promotional Materials.005.png" alt="">
        <!-- </div> -->
      </div>
      <div class="home-content-bottom">
        <p> <a href="http://www.jp-night.com/terms.html" rel="external nofollow" >Terms of Use</a> and <a href="http://www.jp-night.com/privacy.html" rel="external nofollow" >Privacy Policy</a> Please agree to this.</p>
        <div class="content-google">
          <google-recaptcha ref="recaptcha" :sitekey="key" @getValidateCode='getValidateCode' v-model="validateCode"></google-recaptcha>
        </div>
        <div class="login">
          <button :class="isNext ?'login-botton-background':'login-botton'" :disabled="isNext" @click="login()">Shop announcement</button>
        </div> 
      </div>
      
    </div>
  </div>
  
</template>

<script>
import GoogleRecaptcha from '../../common/module/GoogleRecaptcha'
export default {
  data() {
    var checkCode = (rule, value, callback) => {
      if (value == false) {
        callback(new Error('Please perform human-machine verification'));
      } else {
        callback();
      }
    };
    return {
      key: '6Ld9xxgaAAAAAI4xn7seMCmqbxIpVsJ_dwrZu9z1',
      validateCode: false,
      isNext:false
    };
  },
  created(){
    
  },
  mounted(){
    
  },
  components:{
    Google Recaptcha
  },
  methods:{
    login(){
      sessionStorage.setItem('token',true)
      
      this.$router.push({
        path: "/shops",
        query: { out: true }
      })
    },
    getValidateCode(value) {
      console.log(value);
      
      this.isNext = value
    },
  }
};
</script>

<style lang="scss" scoped>
@import "./css/pc.css";
@import "./css/ipad.css";
@import "./css/phone.css";
#rc-anchor-container {
    width: 335px;
}

</style>

3. That’s it, the Google robot is ready to use.

Schematic diagram:

insert image description here

This is the end of this article about the implementation example of vue using Google Recaptcha verification. For more relevant vue Google Recaptcha verification content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Do you really know how to do Vue form validation? vue form validation (form) validate
  • Detailed explanation of how to use Vue Validator, a Vue form validation plugin
  • Problems encountered in Form validation of Vue ElementUI
  • VUE implements image verification code function
  • vue implements the registration function of sending SMS verification code through mobile phone
  • Vue generates a random verification code sample code
  • Element combined with Vue to solve the problem of error prompt when there is value in form validation
  • How to add mobile verification code component function in Vue

<<:  Summary of basic knowledge and operations of MySQL database

>>:  Use nginx + secondary domain name + https support

Recommend

Two ways to introduce svg icons in Vue

How to introduce svg icons in Vue Method 1 of int...

Detailed tutorial on installing mysql 5.7.26 on centOS7.4

MariaDB is installed by default in CentOS, which ...

Detailed tutorial on installing mysql-8.0.20 under Linux

** Install mysql-8.0.20 under Linux ** Environmen...

Solution to MySql service disappearance for unknown reasons

Solution to MySql service disappearance for unkno...

A commonplace technique for implementing triangles using CSS (multiple methods)

In some interview experiences, you can often see ...

Linux file systems explained: ext4 and beyond

Today I will take you through the history of ext4...

How to install Docker on Windows 10 Home Edition

I recently used Docker to upgrade a project. I ha...

Solve the problem of HTML automatic formatting after saving in vscode

The version of vsCode has been updated in recent ...

Web page html special symbols html special characters comparison table

Special symbols Named Entities Decimal encoding S...

Navicat for MySQL tutorial

First, you need to download and install Navicat f...

Detailed explanation of the difference between tags and elements in HTML

I believe that many friends who are new to web pag...

Stealing data using CSS in Firefox

0x00 Introduction A few months ago, I found a vul...

Tutorial on using the frameset tag in HTML

Frameset pages are somewhat different from ordina...