ZF-Commons / ZfcUser

A generic user registration and authentication module for ZF2. Supports Zend\Db and Doctrine2. (Formerly EdpUser)
BSD 3-Clause "New" or "Revised" License
497 stars 343 forks source link

Using Google reCAPTURE #681

Closed jarrettj closed 5 years ago

jarrettj commented 6 years ago

Hi,

Good day.

Has anyone successfully added Google reCAPTURE to login page? Thanks.

Regards. JJ

jarrettj commented 5 years ago

Got it to work.

jroedel commented 5 years ago

Can you post the config you used?

jarrettj commented 5 years ago

Used javascript to enable a submit button if the recapture was clicked. Hope that helps?

login.phtml:

<div id="g-recaptcha"></div>
<input type="hidden" value="<?php echo $googleCaptureKey; ?>" name="gkey">
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
<div class="alert alert-danger captcha-error" style="display: none;"> Fill in the captcha! </div> 
</div>

login.js:

$(document).ready(function() {
    var siteKey = $('input[name="gkey"]').val();
    if (siteKey !== undefined && siteKey !== '') {
        $('form[name="forgot-password"]').submit(function( e ) {
            if(allowSubmit) return true;
            e.preventDefault();
            $('.captcha-error').show();
        });
    }
});

function onloadCallback() {
    var siteKey = $('input[name="gkey"]').val();
    grecaptcha.render('g-recaptcha', {
      'sitekey' : siteKey,
      'callback': capcha_filled,
      'expired-callback': capcha_expired
    });
}

var allowSubmit = false;
function capcha_filled () {
    $('.captcha-error').hide();
    allowSubmit = true;
}
function capcha_expired () {
    allowSubmit = false;
}

Hope that helps somewhat. :)

Regards. JJ