2020-08-17 23:46:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-21 23:16:24 +02:00
|
|
|
final class UserSessionPostController extends AbstractController
|
2020-08-17 23:46:58 +02:00
|
|
|
{
|
2020-08-21 23:16:24 +02:00
|
|
|
protected string $route = '/api/v1/user/session';
|
2020-08-17 23:46:58 +02:00
|
|
|
protected array $mandatoryAttributes = [
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function handle(): void
|
|
|
|
{
|
|
|
|
parent::handle();
|
|
|
|
|
|
|
|
if ($this->response->getStatus() !== ServerStatus::OK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = json_decode($this->requestBody);
|
|
|
|
|
|
|
|
$session = new Session();
|
|
|
|
|
|
|
|
if ($session->IsLoggedIn()) {
|
|
|
|
$this->response = new ApiJsonResponse(ServerStatus::BAD_REQUEST);
|
|
|
|
$this->response->setParameter('success', false);
|
|
|
|
$this->response->setMessage('You are already logged in!');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$session->Login($json->username, $json->password)) {
|
|
|
|
$this->response = new ApiJsonResponse(ServerStatus::UNAUTHORIZED);
|
|
|
|
$this->response->setParameter('success', false);
|
|
|
|
$this->response->setMessage('Login failed!');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response = new ApiJsonResponse();
|
2020-08-21 23:16:24 +02:00
|
|
|
$this->response->setParameter('userId', $session->getUserId());
|
2020-08-17 23:46:58 +02:00
|
|
|
}
|
|
|
|
}
|