slovocast/app/src/Controller/User/LoginUserAction.php

44 lines
1.3 KiB
PHP

<?php
namespace Slovocast\Controller\User;
use Odan\Session\SessionInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Slovocast\Controller\Controller;
use Slovocast\Domain\Repository\User\UserRepositoryInterface;
use Slovocast\Exception\EntityNotFoundException;
use Slovocast\Infrastructure\Api\User\UserAuthorizationInterface;
class LoginUserAction extends Controller
{
public function __construct(
private UserAuthorizationInterface $auth,
private UserRepositoryInterface $userRepository,
private SessionInterface $session
) { }
public function handle(): Response
{
$credentials = $this->request->getParsedBody();
try {
$user = $this->userRepository->getFromEmail($credentials['email']);
} catch (EntityNotFoundException $e) {
$this->session->getFlash()->add('error', "Unable to login user.");
return $this->response->withStatus(400);
}
if (!$this->auth->verify($credentials['password'], $user->getPassword())) {
$this->session->getFlash()->add('error', 'Unable to login.');
return $this->response->withStatus(400);
}
// start the session
$this->session->set('user', [
'id' => $user->getId(),
'authenticated' => true
]);
return $this->response;
}
}