ringfinger/backend/classes/controller/UserSessionPostController.php

44 lines
1023 B
PHP
Raw Normal View History

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