Skip to content

Instantly share code, notes, and snippets.

@jdanyow
Last active November 29, 2017 01:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdanyow/231c18eec8be2fa96169 to your computer and use it in GitHub Desktop.
Save jdanyow/231c18eec8be2fa96169 to your computer and use it in GitHub Desktop.
Google reCAPTCHA with Aurelia
<template>
<require from="./recaptcha"></require>
<form action="?" method="POST">
<recaptcha theme="light" verified.call="onVerified($event)"></recaptcha>
<recaptcha theme="dark" verified.call="onVerified($event)"></recaptcha>
<br>
<input type="submit" value="Submit">
</form>
</template>
export class App {
message = 'hello world';
onVerified(response) {
alert(response);
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/jspm_packages/system.js"></script>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/config.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
import {inject, noView, bindable} from 'aurelia-framework';
const recaptchaCallbackName = 'setRecaptchaReady';
const ready = new Promise(resolve => window[recaptchaCallbackName] = resolve);
// https://developers.google.com/recaptcha/docs/display#explicit_render
let script = document.createElement('script');
script.src = `https://www.google.com/recaptcha/api.js?onload=${recaptchaCallbackName}&render=explicit`;
script.async = true;
script.defer = true;
document.head.appendChild(script);
const sitekey = '6LfwTRkTAAAAAKl5YtVOgNVsENf5TysXLJaZjIen';
@noView()
@inject(Element)
export class Recaptcha {
@bindable verified;
@bindable theme = 'light';
constructor(element) {
this.element = element;
}
attached() {
ready.then(() => grecaptcha.render(this.element, {
sitekey: sitekey,
theme: this.theme,
callback: this.verified
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment