mirror of
https://github.com/opensupports/opensupports.git
synced 2025-07-31 01:35:15 +02:00
Merge branch 'login-function-refactor'
This commit is contained in:
commit
8416c87419
@ -3,30 +3,63 @@
|
|||||||
class LoginController extends Controller {
|
class LoginController extends Controller {
|
||||||
const PATH = '/login';
|
const PATH = '/login';
|
||||||
|
|
||||||
|
private $userInstance;
|
||||||
|
private $session;
|
||||||
|
|
||||||
public function handler() {
|
public function handler() {
|
||||||
$session = Session::getInstance();
|
if ($this->isAlreadyLoggedIn()) {
|
||||||
|
|
||||||
$email = Controller::request('email');
|
|
||||||
$password = Controller::request('password');
|
|
||||||
|
|
||||||
if ($session->sessionExists()) {
|
|
||||||
Response::respondError(ERRORS::SESSION_EXISTS);
|
Response::respondError(ERRORS::SESSION_EXISTS);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$userInstance = User::getUser($email, $password);
|
if ($this->areCredentialsValid()) {
|
||||||
|
$this->createUserSession();
|
||||||
|
|
||||||
if ($userInstance !== null) {
|
Response::respondSuccess($this->getUserData());
|
||||||
$session->createSession($userInstance->id);
|
|
||||||
|
|
||||||
Response::respondSuccess(array(
|
|
||||||
'userId' => $userInstance->id,
|
|
||||||
'userEmail' => $userInstance->email,
|
|
||||||
'userIsAdmin' => $userInstance->admin,
|
|
||||||
'token' => $session->getToken()
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
Response::respondError(ERRORS::INVALID_CREDENTIALS);
|
Response::respondError(ERRORS::INVALID_CREDENTIALS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function isAlreadyLoggedIn() {
|
||||||
|
return $this->getSession()->sessionExists();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function areCredentialsValid() {
|
||||||
|
return ($this->getUserByInputCredentials() !== null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createUserSession() {
|
||||||
|
$this->getSession()->createSession($this->userInstance->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getUserData() {
|
||||||
|
$userInstance = $this->getUserByInputCredentials();
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'userId' => $userInstance->id,
|
||||||
|
'userEmail' => $userInstance->email,
|
||||||
|
'userIsAdmin' => $userInstance->admin,
|
||||||
|
'token' => $this->getSession()->getToken()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getUserByInputCredentials() {
|
||||||
|
if ($this->userInstance === null) {
|
||||||
|
$email = Controller::request('email');
|
||||||
|
$password = Controller::request('password');
|
||||||
|
|
||||||
|
$this->userInstance = User::getUser($email, $password);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->userInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getSession() {
|
||||||
|
if ($this->session === null) {
|
||||||
|
$this->session = Session::getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->session;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user