Skip to content

Instantly share code, notes, and snippets.

@deeja
Created May 21, 2020 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save deeja/ea307cc2671e55bd1194b3ef392cbf1e to your computer and use it in GitHub Desktop.
Save deeja/ea307cc2671e55bd1194b3ef392cbf1e to your computer and use it in GitHub Desktop.
recaptcha V3 Vue or Nuxt
<template>
<div>
<form @submit.prevent="sendMessage" method="post">
<button type="submit">Send</button>
</form>
</div>
</template>
<script>
export default {
mounted() {
let recaptchaScript = document.createElement('script')
recaptchaScript.setAttribute(
'src',
'https://www.google.com/recaptcha/api.js?render=' +
process.env.RECAPTCHA_SITE_KEY
)
document.head.appendChild(recaptchaScript)
},
methods: {
async sendMessage() {
const token = await window.grecaptcha.execute(
process.env.RECAPTCHA_SITE_KEY,
{ action: 'submit' }
)
var bodyFormData = new FormData()
bodyFormData.set('message', this.message)
bodyFormData.set('g-recaptcha-response', token)
const url = "http://someurl.com/endpoint"
await this.$axios
.post(url, bodyFormData, {
headers: { 'Content-Type': 'multipart/form-data' }
})
.then(res => {
if (!res.data.ok) {
throw new Error('failed to send: ' + res.data)
}
})
.catch(error => {
console.error(error)
})
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment