Help putting Google's ReCaptcha 2.0 in a component


import Ember from 'ember';

export default Ember.Component.extend({
  classNames: ['g-recaptcha'],
    attributeBindings: ['data-sitekey'],
    'data-sitekey': '6LcggAqwgTAAA234AAO5J44Z422341rdG1d4qQ234sasfasqDD-iYmVT5p5gv',
  didInsertElement: function() {
    var self = this;
    grecaptcha.render(self.$().prop('departments'), {
      'sitekey': self.get('data-sitekey')



<div id="departments"></div>

Then I just placed it in the form to give me an HTML structure like this:

<div data-sitekey="6LcggAqwgTAAA234AAO5J44Z422341rdG1d4qQ234sasfasqDD-iYmVT5p5gv" class="ember-view g-recaptcha" id="ember705"><div id="departments"></div>


Unfortunately, this renders absolutely nothing. Instead of dynamically loading the script I installed the bower dependency I found named recaptcha-ajax. Through some quick googlefu I have found two related questions found here and here, but still could not get it to work.

Since this is a common use case I am sure many people here have implemented Google’s Click ReCaptcha into their Ember projects. Can anyone point me in the right direction or inform me on how to debug this?

Any help whatsoever is greatly appreciated and will be compensated with good karma. Thank you in advance for your help.

I am really new to ember, and I am taking a guess. Is it a problem with the shadow DOM like this issue in polymer:

The element selector is incorrect in the render part.

You should use grecaptcha.render('departments', {...}) OR grecaptcha.render(self.$('#departments')[0], {...})

@indream Surprisingly both of those, and the code I posted give me the same result which is:

Thanks for replying and trying to help me rectify the problem.

I put together a quick POC that eschews the recaptcha-ajax bower component in favor for the Google API:


import Ember from 'ember';

export default Ember.Component.extend({
  classNames: ['g-recaptcha'],

  attributeBindings: ['siteKey:data-sitekey'],

  siteKey: '6LcggAqwgTAAA234AAO5J44Z422341rdG1d4qQ234sasfasqDD-iYmVT5p5gv',


<!DOCTYPE html>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    {{content-for 'head'}}

    <link rel="stylesheet" href="assets/vendor.css">
    <link rel="stylesheet" href="assets/recaptcha.css">

    {{content-for 'head-footer'}}
    {{content-for 'body'}}

    <script src="assets/vendor.js"></script>
    <script src="" async defer></script>
    <script src="assets/recaptcha.js"></script>

    {{content-for 'body-footer'}}


<h2 id="title">Welcome to Ember.js</h2>


config/environment.js (add to ENV for sake of example, that way we can GET from google)

contentSecurityPolicy: {
  'default-src': "'none'",
  'script-src': "'self'",
  'font-src': "'self'",
  'connect-src': "'self'",
  'img-src': "'self'",
  'style-src': "'self'",
  'media-src': "'self'"

You probably don’t want to load the Google Recaptcha API file like this, but for the proof of concept it’ll do. Also, you probably need to take a closer look at the contentSecurityPolicy stuff for a production environment.

The didInsertElement is unnecessary because we can use attributeBindings to achieve the same end result.

This renders a div that looks like the following:

<div id="ember371" class="ember-view g-recaptcha" data-sitekey="6LcggAqwgTAAA234AAO5J44Z422341rdG1d4qQ234sasfasqDD-iYmVT5p5gv">

The rendered component looks like this: (can’t attach the image directly because I am a new user on the forum). I am using the same sitekey as the one you provided, which is why there is a site key error.

Hope that helps.

1 Like

I think you could also post the console log for us to find out the real cause.

Is there any way to get the data-callback working with this in order to get the token?

I resorted to loading the script async with getScript and then waiting for the object to become available:


Thanks @thermokarst! This was quite helpful. Bryan

1 Like

For those that are interested, ember-cli-google-recaptcha is an easy to use ember addon for adding Google reCAPTCHA to your ember application. It supports having multiple recaptcha components on the same page, and is FastBoot compatible. It also supports v2 and invisible recaptcha widgets:

{{g-recaptcha-v2 verified=(action (mut response)) expired=(action (mut expired) true) reset=reset}}
{{g-recaptcha-invisible verified=(action (mut response)) expired=(action (mut expired) true) reset=reset}}