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 9
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...