Last active
March 22, 2020 03:33
-
-
Save franzwong/e2097282e382846ddad65b2121f7432a to your computer and use it in GitHub Desktop.
HowTo: Integrate Google reCAPTCHA with AWS Cognito
This file contains hidden or 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 axios = require('axios'); | |
// Please replace the property values | |
const config = { | |
recaptcha: { | |
secretKey: '<Your reCAPTCHA secret key>', | |
}, | |
}; | |
exports.handler = async (event) => { | |
if (!event.request.validationData) { | |
throw new Error('Missing validation data'); | |
} | |
try { | |
const payload = { | |
secret: config.recaptcha.secretKey, | |
response: event.request.validationData.recaptchaToken, | |
remoteip: undefined, // Optional. The user's IP address. | |
}; | |
const verifyResponse = await axios({ | |
method: 'post', | |
url: 'https://www.google.com/recaptcha/api/siteverify', | |
params: payload, | |
}); | |
console.log(JSON.stringify(verifyResponse.data)); | |
if (verifyResponse.data.success) { | |
event.response.autoConfirmUser = true; | |
return event; | |
} else { | |
throw new Error('Recaptcha verification failed'); | |
} | |
} catch (error) { | |
console.error(error); | |
throw error; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment