Jump to content

How to attach google recaptcha in default pw admin login form ?


adrianmak
 Share

Recommended Posts

  • 3 weeks later...

Hi @adrianmak

You can achieve this by using hooks and my ReCaptcha module.

 

First install the MarkupGoogleReCaptcha module then in file ready.php, write the following code :

/*
 *  replace the LAST occurence of $search by $replace in $subject
 */
function str_lreplace($search, $replace, $subject) {

    return preg_replace('~(.*)' . preg_quote($search, '~') . '~', '$1' . $replace, $subject, 1);
}
/*
 *  replace the FIRST occurence of $search by $replace in $subject
 */
function str_freplace($search, $replace, $subject) {

    $from = '/'.preg_quote($search, '/').'/';
    return preg_replace($from, $replace, $subject, 1);
}

$captchamod = wire('modules')->get("MarkupGoogleRecaptcha");

wire()->addHookProperty('Page::captcha', function($event) use ($captchamod) {
    $event->return = $captchamod;
});

wire()->addHookAfter('Page::render', function($event) {

    $template = $event->object->template;
    $page = $event->object;

    if ($template == 'admin' && !wire('user')->isLoggedin()) {
        $captchaScript = $page->captcha->getScript() . '</body>';
        $captchaHtml = $page->captcha->render() . '</form>';
        $event->return = str_freplace('</form>', $captchaHtml, $event->return);
        $event->return = str_lreplace('</body>', $captchaScript, $event->return);
    }
});

wire()->addHookAfter('Session::authenticate', function($event) {

    $page = wire('page');
    $template = $page->template;

    if ($template == 'admin') {
        if ($page->captcha->verifyResponse() == false) {
            wire('session')->logout();
            wire('session')->redirect(wire('config')->urls->admin);
        }
    }
});

 

admin-recaptcha.png.d68de0d46d449b3f5bfb7b868a580862.png

  • Like 10
Link to comment
Share on other sites

  • 7 years later...

Hi

I'm trying to do the same thing, but it doesn't seem to work anymore.

The ReCaptcha appears, but when I submit the login, the page reloads, but the session does not start. I'm using the invisible ReCaptcha, I don't know if that affects it

Edited by Laegnur
Link to comment
Share on other sites

Hi @Laegnur I suggest you to use @nbcommunication Cloudflare turnstile module if it’s a valid option or @MoritzLost hCaptcha module as my module was not updated for a while, see:

—-

ps: you can get an introduction on  https://weekly.pw/issue/542/

—-

About the hook I writen above it may still work, but you have to debug it a bit. The issue you described seem to happen in 

verifyResponse()

check your dev console by checking `Preserve log` on network tab to be able to check whats going on after a  page reload.

Edited by flydev
added pw weekly issue reference
Link to comment
Share on other sites

Hi,

I've not tried this implementation before with any captcha module, so not sure why it would no longer work, but I would point out that we're still using @flydev's MarkupGoogleReCaptcha on many sites (front-end, v2) without issue, and the recent MarkupCloudflareTurnstile is effectively the same implementation, but with the CF captcha, so if it works for one, it should work for the other.

Cheers,

Chris

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...