adrianmak Posted July 19, 2017 Share Posted July 19, 2017 For custom form, it is easy to add a recpatcha. But how about on default admin login form ? Link to comment Share on other sites More sharing options...
flydev Posted August 4, 2017 Share Posted August 4, 2017 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); } } }); 10 Link to comment Share on other sites More sharing options...
Laegnur Posted October 22, 2024 Share Posted October 22, 2024 (edited) 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 October 22, 2024 by Laegnur Link to comment Share on other sites More sharing options...
flydev Posted October 23, 2024 Share Posted October 23, 2024 (edited) 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 October 23, 2024 by flydev added pw weekly issue reference Link to comment Share on other sites More sharing options...
nbcommunication Posted October 25, 2024 Share Posted October 25, 2024 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now