
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
react-google-recaptcha3
Advanced tools
React service for Google reCAPTCHA 3
See Demo
Run following command to install react-google-recaptcha3
npm i react-google-recaptcha3
reCAPTCHA v3 introduces a new concept: actions. When you specify an action name in each place you execute reCAPTCHA you enable two new features:
At first you need to import ReactRecaptcha3
to your component
import ReactRecaptcha3 from 'react-google-recaptcha3';
Pass your siteKey in useEffect
hook
useEffect(() => {
ReactRecaptcha3.init(YOUR_SITE_KEY)
}, [])
The init
function will return Promise with status
parameter that will indicate script loaded status
useEffect(() => {
ReactRecaptcha3.init(YOUR_SITE_KEY).then(status => {
// status: success/error
// success - script is loaded and greaptcha is ready
// error - script is not loaded
console.log(status)
})
}, [])
On form submit generate recaptcha token (it will be checked in backend) using siteKey
const handleSubmit = () => {
ReactRecaptcha3.getToken().then(token => {
console.log(token)
// send token with form data
// dataToSend.token = token
// fetch(url, { method: 'POST', body: JSON.stringify(dataToSend) })
}, error => {
// handle error here
console.log(error)
})
}
Execute getToken
with action name. See more here
ReactRecaptcha3.getToken({ action: 'homepage' })
Destroy recaptcha
ReactRecaptcha3.destroy()
In backend we need to verify given token using secretKey.
const request = require('request-promise');
const secretKey = YOUR_RECAPTCHA_SECRET_KEY;
const userIp = 'USER_IP';
request.get(
{
url: `https://www.google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${recaptchaToken}&remoteip=${userIp}`,
}).then((response) => {
// If response false return error message
if (response.success === false) {
return res.json({success: false, error: 'Recaptcha token validation failed'});
}
// otherwise continue handling/saving form data
next();
})
$recaptchaToken = isset($_POST['recaptchaToken']) ? $_POST['recaptchaToken'] : false;
if(!$recaptchaToken) {
//Do something with error
}
$secretKey = YOUR_RECAPTCHA_SECRET_KEY;
$userIp = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$recaptchaToken."&remoteip=".$userIp);
if($response.success == false){
//Do something with error
} else {
// reCaptchaToken is valid you can continue with the rest of your code
}
FAQs
React service for google recptcha3
The npm package react-google-recaptcha3 receives a total of 520 weekly downloads. As such, react-google-recaptcha3 popularity was classified as not popular.
We found that react-google-recaptcha3 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.