Skip to content

Instantly share code, notes, and snippets.

@allenhwkim
Created October 12, 2024 16:54
Show Gist options
  • Save allenhwkim/1c59faeacb4119d9702fe3af635b93b9 to your computer and use it in GitHub Desktop.
Save allenhwkim/1c59faeacb4119d9702fe3af635b93b9 to your computer and use it in GitHub Desktop.
Google reCAPTCHA v3 server/browser
<script src="https://www.google.com/recaptcha/api.js?render={{KEY}}"></script>
<script>
grecaptcha.ready(async () => {
const token = await grecaptcha.execute('{{KEY}}', {action: 'demo'});
document.querySelector('#token').value = token;
});
</script>
<h1>Google3 reCAPTCHA v3 Demo</h1>
<input id="hello" value="Hello World" />
<textarea hidden id="token"></textarea>
<button onclick="onSubmit()">Submit</button>
<script>
function onSubmit() {
var hello = document.querySelector('#hello').value;
var token = document.querySelector('#token').value;
fetch('/send', {
headers: { 'Content-Type': 'application/json' },
method: 'post',
body: JSON.stringify({ hello: hello, token: token })
})
.then(response => response.text())
.then(text => alert(text))
.catch(error => alert(text));
}
</script>
const express = require('express');
const axios = require('axios');
const app = express();
app.use(express.static('public'));
app.use(express.json());
app.post('/send', (req, res) => {
const token = req.body.token;
const url = `https://www.google.com/recaptcha/api/siteverify?secret={{SECRET}}&response=${token}`;
axios.post(url)
.then(resp => res.json(resp.data))
.catch(err => res.json(err))
});
app.listen(3000, () => console.log(`Listening on port 3000`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment