Added new callback to the account controller to validate usernames

This commit is contained in:
Alex Tselegidis 2021-12-18 18:55:58 +01:00
parent f58d3b142b
commit a3282febc9
2 changed files with 43 additions and 1 deletions

View file

@ -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);
}
}
}

View file

@ -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
};
})();