Refactored the login controller callback

This commit is contained in:
Alex Tselegidis 2021-11-06 16:09:29 +01:00
parent c9a7f551b5
commit 31b452766d

View file

@ -11,10 +11,6 @@
* @since v1.0.0 * @since v1.0.0
* ---------------------------------------------------------------------------- */ * ---------------------------------------------------------------------------- */
use EA\Engine\Notifications\Email as EmailClient;
use EA\Engine\Types\Email;
use EA\Engine\Types\NonEmptyText;
/** /**
* User controller * User controller
* *
@ -31,6 +27,7 @@ class User extends EA_Controller {
parent::__construct(); parent::__construct();
$this->load->library('accounts'); $this->load->library('accounts');
$this->load->library('email_messages');
} }
/** /**
@ -95,41 +92,37 @@ class User extends EA_Controller {
{ {
try try
{ {
if ( ! request('username') || ! request('password'))
{
throw new Exception('Invalid credentials given!');
}
$username = request('username'); $username = request('username');
if (empty($username))
{
throw new InvalidArgumentException('Empty username provided.');
}
$password = request('password'); $password = request('password');
if (empty($password))
{
throw new InvalidArgumentException('Empty password provided.');
}
$user_data = $this->accounts->check_login($username, $password); $user_data = $this->accounts->check_login($username, $password);
if ($user_data) if (empty($user_data))
{ {
throw new InvalidArgumentException('Invalid credentials provided, please try again.');
}
session($user_data); // Save data in the session. session($user_data); // Save data in the session.
$response = AJAX_SUCCESS; json_encode([
} 'success' => TRUE,
else ]);
{
$response = AJAX_FAILURE;
}
} }
catch (Throwable $e) catch (Throwable $e)
{ {
$this->output->set_status_header(500); json_exception($e);
$response = [
'message' => $e->getMessage(),
'trace' => config('debug') ? $e->getTrace() : []
];
} }
$this->output
->set_content_type('application/json')
->set_output(json_encode($response));
} }
/** /**