49 lines
1.7 KiB
PHP
49 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Slovocast\Handler\User;
|
|
|
|
use Odan\Session\SessionInterface;
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Log\LoggerInterface;
|
|
use Slovocast\Handler\Handler;
|
|
use Slovocast\Domain\Repository\User\UserRepositoryInterface;
|
|
use Slovocast\Exception\EntityNotFoundException;
|
|
use Slovocast\Infrastructure\Api\User\UserAuthorizationInterface;
|
|
|
|
class LoginUserAction extends Handler
|
|
{
|
|
public function __construct(
|
|
private UserAuthorizationInterface $auth,
|
|
private UserRepositoryInterface $userRepository,
|
|
private SessionInterface $session,
|
|
private LoggerInterface $logger,
|
|
) { }
|
|
|
|
public function handle(): Response
|
|
{
|
|
$credentials = $this->request->getParsedBody();
|
|
|
|
try {
|
|
$user = $this->userRepository->getFromEmail($credentials['email']);
|
|
} catch (EntityNotFoundException $e) {
|
|
$this->logger->error("Unable to login user.");
|
|
$this->session->getFlash()->add('error', "Unable to login user.");
|
|
|
|
return $this->render('user/login.twig')->withStatus(400);
|
|
}
|
|
|
|
if (!$this->auth->verify($credentials['password'], $user->getPassword())) {
|
|
$this->logger->error("Unable to verify user password.");
|
|
$this->session->getFlash()->add('error', "Unable to login user.");
|
|
|
|
return $this->render('user/login.twig')->withStatus(400);
|
|
}
|
|
|
|
// start the session
|
|
$this->session->getFlash()->add('success', "Successfully logged in.");
|
|
$this->session->set('authenticated', true);
|
|
$this->session->set('user', $user->toArray());
|
|
return $this->redirect('/dashboard', 302);
|
|
}
|
|
}
|