ringfinger/backend/classes/controller/UserSessionPostController.php

40 lines
988 B
PHP

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