Skip to content

Instantly share code, notes, and snippets.

@divneet
Created October 4, 2015 17:38
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 divneet/72fa8799138432eee0c5 to your computer and use it in GitHub Desktop.
Save divneet/72fa8799138432eee0c5 to your computer and use it in GitHub Desktop.
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);
console.log("running on port 3000");
var SECRET = "6Ld4*******";
// 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=g-recaptcha-response", 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": "captcha",
"version": "0.0.0",
"private": true,
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"body-parser": "~1.13.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"consolidate": "0.13.1",
"hogan.js": "3.0.2",
"express": "~4.13.1",
"jade": "~1.11.0",
"morgan": "~1.6.1",
"serve-favicon": "~2.3.0"
}
}
function verifyRecaptcha(key, callback) {
https.get("https://www.google.com/recaptcha/api/siteverify?secret=" + SECRET + "&response=g-recaptcha-response", function(res) {
var data = "";
res.on('data', function (chunk) {
data += chunk.toString();
});
res.on('end', function() {
try {
var parsedData = JSON.parse(data);
console.log(parsedData);
callback(parsedData.success);
} catch (e) {
callback(false);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment