From a3282febc9b3e70e494b2319fbe7ac001808c06d Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 18 Dec 2021 18:55:58 +0100 Subject: [PATCH] Added new callback to the account controller to validate usernames --- application/controllers/Account.php | 23 +++++++++++++++++++++++ assets/js/http/account_http_client.js | 21 ++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/application/controllers/Account.php b/application/controllers/Account.php index 47fb9c15..da593cf2 100644 --- a/application/controllers/Account.php +++ b/application/controllers/Account.php @@ -102,4 +102,27 @@ class Account extends EA_Controller { json_exception($e); } } + + /** + * Make sure the username is valid and unique in the database. + */ + public function validate_username() + { + try + { + $username = request('username'); + + $user_id = request('user_id'); + + $is_valid = $this->users_model->validate_username($username, $user_id); + + json_response([ + 'is_valid' => $is_valid, + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } } diff --git a/assets/js/http/account_http_client.js b/assets/js/http/account_http_client.js index 10cee5c7..3ff9d464 100644 --- a/assets/js/http/account_http_client.js +++ b/assets/js/http/account_http_client.js @@ -28,7 +28,26 @@ App.Http.Account = (function () { return $.post(url, data); } + /** + * Validate username. + * + * @param {String} username + * + * @return {Object} + */ + function validateUsername(username) { + const url = App.Utils.Url.siteUrl('account/validate_username'); + + const data = { + csrf_token: App.Vars.csrf_token, + username + }; + + return $.post(url, data); + } + return { - save + save, + validateUsername }; })();