Simple function for requesting tokens to the Google ReCaptcha v3 API. I want to thanks Codeep for writting https://github.com/codeep/react-recaptcha-v3/, without it I would't be able to easily write this lib, thank you!
ReCaptcha v3 is still in beta, hence this lib too. The final API may change in the future.
yarn add @dimax-ar/recaptcha
npm install --save @dimax-ar/recaptcha
import { loadReCaptcha } from '@dimax-ar/recaptcha';
/**
* Please, save your key in an environmental variable
* instead of pasting it in the code, since can vary between
* development and production environments.
*
* @param {String} key The recaptcha v3 client side key
* @return {void}
*/
loadReCaptcha(process.env.YOUR_CATPCHA_CLIENT_KEY);
import reCaptcha from '@dimax-ar/recaptcha';
/**
* @param {String} action
* The action the user is going to do in the page/section
* @param {(token) => void} handleVerify
* The callback with the new token.
*/
reCaptcha('action', handleVerify);
- ReCaptcha docs: https://developers.google.com/recaptcha/docs/v3
- ReCaptcha registration: https://www.google.com/recaptcha/admin#v3signup
This function must be called once in the entry file.
// entry file
import { loadReCaptcha } from '@dimax-ar/recaptcha';
loadReCaptcha(process.env.YOUR_CATPCHA_CLIENT_KEY);
import reCaptcha from '@dimax-ar/recaptcha';
export default {
created() {
reCaptcha('example-action', this.handleVerify);
},
methods: {
handleVerify(token) {
console.log(token);
},
},
};
import React from 'react';
import reCaptcha from '@dimax-ar/recaptcha';
class Example extends React.Component {
constructor() {
super();
reCaptcha('example-action', this.handleVerify);
}
handleVerify = (token) => {
console.log(token);
}
}