Skip to content

Instantly share code, notes, and snippets.

@jaxbot
Created December 9, 2014 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaxbot/1ef9ecb72bd0df1d4f24 to your computer and use it in GitHub Desktop.
Save jaxbot/1ef9ecb72bd0df1d4f24 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<h1>Register for test site</h1>
<form action="register" method="post">
<input type="text" name="username" placeholder="Username"><br>
<input type="text" name="email" placeholder="Email"><br>
<input type="password" name="password" placeholder="Password"><br>
<div class="g-recaptcha" data-sitekey="sitekey here"></div>
<input type="submit" value="Register">
</form>
</body>
</html>
var express = require('express');
var bodyParser = require('body-parser');
var engines = require('consolidate');
var app = express();
var https = require('https');
app.use(bodyParser.urlencoded({ extended: false }));
app.engine('html', engines.hogan);
app.get('/', function(req, res) {
res.render('form.html');
});
app.post('/register', function(req, res) {
verifyRecaptcha(req.body["g-recaptcha-response"], function(success) {
if (success) {
res.end("Success!");
// TODO: do registration using params in req.body
} else {
res.end("Captcha failed, sorry.");
// TODO: take them back to the previous page
// and for the love of everyone, restore their inputs
}
});
});
app.listen(3000);
var SECRET = "secret from webpage here";
// Helper function to make API call to recatpcha and check response
function verifyRecaptcha(key, callback) {
https.get("https://www.google.com/recaptcha/api/siteverify?secret=" + SECRET + "&response=" + key, function(res) {
var data = "";
res.on('data', function (chunk) {
data += chunk.toString();
});
res.on('end', function() {
try {
var parsedData = JSON.parse(data);
callback(parsedData.success);
} catch (e) {
callback(false);
}
});
});
}
{
"name": "nodejs-recaptcha",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"consolidate": "^0.10.0",
"express": "^4.10.4",
"hogan.js": "^3.0.2"
}
}
@pankajkrr
Copy link

Here you can find the complete demo. Go through the link:
https://jsonworld.com/demo/45/nodejs/Implementing-google-reCAPTHCA-in--node.js-application

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment