Created
March 22, 2020 03:28
-
-
Save franzwong/1eb4d1dcd0afe185aae3014d77a9bbc0 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 Auth = require('@aws-amplify/auth').default; | |
// Please replace the property values | |
const config = { | |
aws: { | |
region: '<Your aws region>', | |
cognito: { | |
userPoolId: '<Your Cognito user pool ID>', | |
userPoolWebClientId: '<Your Cognito user pool web client ID>', | |
}, | |
}, | |
recaptcha: { | |
secretKey: '<Your reCAPTCHA secret key>', | |
}, | |
}; | |
// Configure Amplify | |
Auth.configure({ | |
region: config.aws.region, | |
userPoolId: config.aws.cognito.userPoolId, | |
userPoolWebClientId: config.aws.cognito.userPoolWebClientId, | |
}); | |
grecaptcha.ready(function() { | |
document.getElementById("signup-form").addEventListener("submit", signUp); | |
}); | |
async function signUp(event) { | |
event.preventDefault(); | |
const form = event.target; | |
const username = form.elements['username'].value; | |
const password = form.elements['password'].value; | |
try { | |
const recaptchaToken = await grecaptcha.execute(config.recaptcha.siteKey, {action: 'signUp'}); | |
await Auth.signUp({ | |
username, | |
password, | |
validationData: [{ | |
Name: 'recaptchaToken', | |
Value: recaptchaToken, | |
}], | |
}); | |
alert('You\'ve signed up'); | |
} catch (err) { | |
alert(err.message || JSON.stringify(err)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment