如何在vuetify上添加Google Recaptcha 3?

时间:2019-10-23 08:55:18

标签: vue.js vue-component recaptcha vuetify.js react-google-recaptcha

我想在我的vuetify项目中添加google recaptcha 3

像这样的Google Recaptcha 3:

enter image description here

我从这里获得参考:https://www.npmjs.com/package/vue-recaptcha-v3

首先:我运行npm install vue-recaptcha-v3

第二:我像这样修改main.js:

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './registerServiceWorker'
import vuetify from './plugins/vuetify'
import { VueReCaptcha } from 'vue-recaptcha-v3'

Vue.config.productionTip = false
Vue.use(VueReCaptcha, {
  siteKey: '<site key>',
  loaderOptions: {
    useRecaptchaNet: true
  }
})

new Vue({
  router,
  store,
  vuetify,
  render: h => h(App),
  methods: {
    recaptcha() {
      this.$recaptcha('login').then((token) => {
        console.log(token) // Will print the token
      })
    }
  },
  template: '<button @click="recaptcha">Execute recaptcha</button>'
}).$mount('#app')

我的问题是我很困惑从组件中调用它

我的Vue组件是这样的:

<template>
  <v-container>
    ...
        <v-row>
          <v-col
            cols="6"
          >
            <v-form v-model="valid" ref="form" 
            >
              <v-text-field
              label="Full Name"
              outlined
              v-model="fullname"
              ></v-text-field>
              <v-text-field
              label="Email"
              outlined
              v-model="email"
              ></v-text-field>
              <v-text-field
              label="Phone"
              outlined
              v-model="phone"
              ></v-text-field>

              <v-row justify="center">
                <v-btn color="green accent-4" :dark="valid" @click="validate()" :disabled="!valid">Save
                  <v-icon dark right>mdi-checkbox-marked-circle</v-icon>
                </v-btn>
              </v-row>
            </v-form>
          </v-col>
        </v-row>
      ...
  </v-container>
</template>

<script>
  export default {
    data: () => ({

    })
  }
</script>

如何从组件中调用google recaptcha 3?

1 个答案:

答案 0 :(得分:1)

添加网站后,您将从管理控制台获得两个令牌。

在Vue应用程序中使用客户端密钥,在后端使用服务器密钥

  

在main.js中添加客户端密码

Vue.use(VueReCaptcha, {
  siteKey: '<site key>'
}
  

删除main.js中的方法,仅用于演示目的

new Vue({,
  router,
  store,
  vuetify,
  render: h => h(App),
}).$mount('#app')

在任何事件中添加重新录制

  

在Vue组件中添加recaptcha

<template>
 <button @click="recaptcha">Recaptcha</button>
</template>

<script>
  export default {
   ...
   methods: {
   ...
   recaptcha() {
      this.$recaptcha('login').then((token) => {
        console.log(token) // Will print the token
      })
    }
   },
   ....
  }
</script>

它返回登录令牌,您可以使用该令牌将发帖请求发送到recaptcha to verify the user