Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| AuthController | |
0.00% |
0 / 25 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| loginForm | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| loginSubmit | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
| logout | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Modules\User\Presentation\Web; |
| 6 | |
| 7 | use App\Modules\User\Application\Command\LoginCommand; |
| 8 | use App\Modules\User\Application\Command\LoginHandler; |
| 9 | use Psr\Http\Message\ResponseFactoryInterface; |
| 10 | use Psr\Http\Message\ResponseInterface; |
| 11 | use Psr\Http\Message\ServerRequestInterface; |
| 12 | use Yiisoft\User\CurrentUser; |
| 13 | use Twig\Environment as TwigEnvironment; |
| 14 | |
| 15 | /** |
| 16 | * Authentication Controller for Guest Zone. |
| 17 | * |
| 18 | * Handles login form rendering, credential verification, and user logout. |
| 19 | * |
| 20 | * @package App\Modules\User\Presentation\Web |
| 21 | */ |
| 22 | final readonly class AuthController |
| 23 | { |
| 24 | /** |
| 25 | * AuthController constructor. |
| 26 | * |
| 27 | * @param ResponseFactoryInterface $responseFactory PSR-7 Response factory. |
| 28 | * @param TwigEnvironment $twig Twig template environment. |
| 29 | * @param LoginHandler $loginHandler Login command handler. |
| 30 | * @param CurrentUser $currentUser Yii3 CurrentUser service instance. |
| 31 | */ |
| 32 | public function __construct( |
| 33 | private ResponseFactoryInterface $responseFactory, |
| 34 | private TwigEnvironment $twig, |
| 35 | private LoginHandler $loginHandler, |
| 36 | private CurrentUser $currentUser |
| 37 | ) { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Renders login form for guest users. |
| 42 | * |
| 43 | * @return ResponseInterface PSR-7 HTML response containing login form. |
| 44 | */ |
| 45 | public function loginForm(): ResponseInterface |
| 46 | { |
| 47 | $html = $this->twig->render('auth/login.twig', [ |
| 48 | 'error' => null, |
| 49 | ]); |
| 50 | |
| 51 | $response = $this->responseFactory->createResponse(200); |
| 52 | $response->getBody()->write($html); |
| 53 | return $response->withHeader('Content-Type', 'text/html; charset=utf-8'); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Handles submitted login credentials. |
| 58 | * |
| 59 | * @param ServerRequestInterface $request PSR-7 Server request. |
| 60 | * @return ResponseInterface Redirect response on success or HTML response with error. |
| 61 | */ |
| 62 | public function loginSubmit(ServerRequestInterface $request): ResponseInterface |
| 63 | { |
| 64 | /** @var array<string, mixed> $parsedBody */ |
| 65 | $parsedBody = (array)($request->getParsedBody() ?? []); |
| 66 | $login = (string)($parsedBody['login'] ?? ''); |
| 67 | $password = (string)($parsedBody['password'] ?? ''); |
| 68 | |
| 69 | $command = new LoginCommand($login, $password); |
| 70 | $user = $this->loginHandler->handle($command); |
| 71 | |
| 72 | if ($user === null) { |
| 73 | $html = $this->twig->render('auth/login.twig', [ |
| 74 | 'error' => 'Invalid login or password.', |
| 75 | ]); |
| 76 | $response = $this->responseFactory->createResponse(400); |
| 77 | $response->getBody()->write($html); |
| 78 | return $response->withHeader('Content-Type', 'text/html; charset=utf-8'); |
| 79 | } |
| 80 | |
| 81 | $this->currentUser->login(new Identity($user->getId() ?? 0)); |
| 82 | |
| 83 | $response = $this->responseFactory->createResponse(302); |
| 84 | return $response->withHeader('Location', '/dashboard'); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Logs out current user and redirects to login page. |
| 89 | * |
| 90 | * @return ResponseInterface Redirect response to /login. |
| 91 | */ |
| 92 | public function logout(): ResponseInterface |
| 93 | { |
| 94 | $this->currentUser->logout(); |
| 95 | |
| 96 | $response = $this->responseFactory->createResponse(302); |
| 97 | return $response->withHeader('Location', '/login'); |
| 98 | } |
| 99 | } |