如何在 vuetify 上添加 google recaptcha 3?

Suc*_*Man 3 recaptcha vue.js vue-component vuetify.js react-google-recaptcha

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

谷歌 recaptcha 3 像这样:

在此处输入图片说明

我从这里得到参考: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')
Run Code Online (Sandbox Code Playgroud)

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

我的 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>
Run Code Online (Sandbox Code Playgroud)

如何从我的组件调用 google recaptcha 3?

cha*_*ans 6

添加站点时,您会从管理控制台获得两个令牌。

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

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

Vue.use(VueReCaptcha, {
  siteKey: '<site key>'
}
Run Code Online (Sandbox Code Playgroud)

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

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

在任何事件上添加recaptcha

在你的 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>
Run Code Online (Sandbox Code Playgroud)

它返回登录令牌,您可以使用它向recaptcha发送post请求以验证用户