Created
July 28, 2019 21:54
-
-
Save pedrosancao/4893654efc2092b360f8093ecb58c3c3 to your computer and use it in GitHub Desktop.
validate captcha and show data for google cloud functions with node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https') | |
const querystring = require('querystring') | |
exports.showdata = (req, res) => { | |
const data = '___________________' | |
const captchaSecret = '________________________________' | |
const allowedOrigins = ['______________________________'] | |
const verifyData = querystring.stringify({ | |
secret : captchaSecret, | |
response : req.query.response || req.body.response, | |
remoteip : req.ip | |
}) | |
const verifyReq = https.request({ | |
hostname: 'www.google.com', | |
path: '/recaptcha/api/siteverify', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': Buffer.byteLength(verifyData) | |
} | |
}, verifyRes => { | |
let responseData = '' | |
verifyRes.on('data', d => { | |
responseData += d | |
}) | |
verifyRes.on('end', () => { | |
let response = JSON.parse(responseData) | |
console.log(response); | |
if (!response.success) { | |
res.status(401).send('unauthorized') | |
} else { | |
res.status(200).send(data) | |
} | |
}) | |
}) | |
verifyReq.on('error', (error) => { | |
console.error(error) | |
res.status(401).send('unauthorized') | |
}) | |
verifyReq.write(verifyData) | |
verifyReq.end() | |
if (allowedOrigins.indexOf(req.get('origin')) >= 0) { | |
res.header('Access-Control-Allow-Origin', '*') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment