From 8b79ab82b4737f8555b35695bd268a761bf8edfc Mon Sep 17 00:00:00 2001 From: Roman Hergenreder Date: Thu, 2 Jul 2020 00:59:55 +0200 Subject: [PATCH] Google reCaptcha --- core/Views/Account/AccountView.class.php | 16 ++++++ core/Views/Account/Register.class.php | 10 ---- js/account.js | 68 ++++++++++++++++-------- 3 files changed, 61 insertions(+), 33 deletions(-) diff --git a/core/Views/Account/AccountView.class.php b/core/Views/Account/AccountView.class.php index b692b28..126e93e 100644 --- a/core/Views/Account/AccountView.class.php +++ b/core/Views/Account/AccountView.class.php @@ -16,6 +16,16 @@ abstract class AccountView extends View { $this->icon = "image"; } + public function loadView() { + parent::loadView(); + + $document = $this->getDocument(); + $settings = $document->getUser()->getConfiguration()->getSettings(); + if ($settings->isRecaptchaEnabled()) { + $document->getHead()->loadGoogleRecaptcha($settings->getRecaptchaSiteKey()); + } + } + public function getCode() { $html = parent::getCode(); @@ -38,6 +48,12 @@ abstract class AccountView extends View { "; + $settings = $this->getDocument()->getUser()->getConfiguration()->getSettings(); + if ($settings->isRecaptchaEnabled()) { + $siteKey = $settings->getRecaptchaSiteKey(); + $html .= ""; + } + return $html; } diff --git a/core/Views/Account/Register.class.php b/core/Views/Account/Register.class.php index f1d2ab4..3cb384a 100644 --- a/core/Views/Account/Register.class.php +++ b/core/Views/Account/Register.class.php @@ -14,16 +14,6 @@ class Register extends AccountView { $this->icon = "user-plus"; } - public function loadView() { - parent::loadView(); - - $document = $this->getDocument(); - $settings = $document->getUser()->getConfiguration()->getSettings(); - if ($settings->isRecaptchaEnabled()) { - $document->getHead()->loadGoogleRecaptcha($settings->getRecaptchaSiteKey()); - } - } - public function getAccountContent() { $settings = $this->getDocument()->getUser()->getConfiguration()->getSettings(); diff --git a/js/account.js b/js/account.js index 968db8c..d5494d5 100644 --- a/js/account.js +++ b/js/account.js @@ -11,6 +11,21 @@ $(document).ready(function () { $("#alertMessage").hide(); } + function submitForm(btn, method, params, onSuccess) { + let textBefore = btn.text(); + btn.prop("disabled", true); + btn.html("Submitting… ") + jsCore.apiCall(method, params, (res) => { + btn.prop("disabled", false); + btn.text(textBefore); + if (!res.success) { + showAlert("danger", res.msg); + } else { + onSuccess(); + } + }); + } + // Login $("#btnLogin").click(function() { const username = $("#username").val(); @@ -44,27 +59,30 @@ $(document).ready(function () { let email = $("#email").val().trim(); let password = $("#password").val(); let confirmPassword = $("#confirmPassword").val(); + let siteKey = $("#siteKey").val().trim(); if (username === '' || email === '' || password === '' || confirmPassword === '') { showAlert("danger", "Please fill out every field."); } else if(password !== confirmPassword) { showAlert("danger", "Your passwords did not match."); } else { - let textBefore = btn.text(); let params = { username: username, email: email, password: password, confirmPassword: confirmPassword }; - - btn.prop("disabled", true); - btn.html("Submitting… ") - jsCore.apiCall("user/register", params, (res) => { - btn.prop("disabled", false); - btn.text(textBefore); - if (!res.success) { - showAlert("danger", res.msg); - } else { + if (typeof grecaptcha !== 'undefined') { + grecaptcha.ready(function() { + grecaptcha.execute(siteKey, {action: 'register'}).then(function(captcha) { + params["captcha"] = captcha; + submitForm(btn, "user/register", params, () => { + showAlert("success", "Account successfully created, check your emails."); + $("input").val(""); + }); + }); + }); + } else { + submitForm(btn, "user/register", params, () => { showAlert("success", "Account successfully created, check your emails."); $("input").val(""); - } - }); + }); + } } }); @@ -105,19 +123,23 @@ $(document).ready(function () { let btn = $(this); let email = $("#email").val(); - let textBefore = btn.text(); - btn.prop("disabled", true); - btn.html("Submitting… ") - jsCore.apiCall("user/requestPasswordReset", { email: email }, (res) => { - btn.prop("disabled", false); - btn.text(textBefore); - if (!res.success) { - showAlert("danger", res.msg); - } else { + let params = { email: email }; + if (typeof grecaptcha !== 'undefined') { + grecaptcha.ready(function() { + grecaptcha.execute(siteKey, {action: 'resetPassword'}).then(function(captcha) { + params["captcha"] = captcha; + submitForm(btn, "user/requestPasswordReset", params, () => { + showAlert("success", "If the e-mail address exists and is linked to a account, you will receive a password reset token."); + $("input").val(""); + }); + }); + }); + } else { + submitForm(btn, "user/requestPasswordReset", params, () => { showAlert("success", "If the e-mail address exists and is linked to a account, you will receive a password reset token."); $("input").val(""); - } - }); + }); + } }); $("#btnResetPassword").click(function (e) {