|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Auth\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use App\Auth\AuthService; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
12
|
|
|
use Psr\Log\LoggerInterface; |
|
13
|
|
|
use Throwable; |
|
14
|
|
|
use Yiisoft\Http\Method; |
|
15
|
|
|
use Yiisoft\Http\Status; |
|
16
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
|
17
|
|
|
use Yiisoft\Yii\View\ViewRenderer; |
|
18
|
|
|
|
|
19
|
|
|
final class AuthController |
|
20
|
|
|
{ |
|
21
|
|
|
private ResponseFactoryInterface $responseFactory; |
|
22
|
|
|
private LoggerInterface $logger; |
|
23
|
|
|
private UrlGeneratorInterface $urlGenerator; |
|
24
|
|
|
private ViewRenderer $viewRenderer; |
|
25
|
|
|
private AuthService $authService; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
ResponseFactoryInterface $responseFactory, |
|
29
|
|
|
ViewRenderer $viewRenderer, |
|
30
|
|
|
LoggerInterface $logger, |
|
31
|
|
|
UrlGeneratorInterface $urlGenerator, |
|
32
|
|
|
AuthService $authService |
|
33
|
|
|
) { |
|
34
|
|
|
$this->responseFactory = $responseFactory; |
|
35
|
|
|
$this->logger = $logger; |
|
36
|
|
|
$this->urlGenerator = $urlGenerator; |
|
37
|
|
|
$this->viewRenderer = $viewRenderer->withControllerName('auth'); |
|
38
|
|
|
$this->authService = $authService; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function login(ServerRequestInterface $request): ResponseInterface |
|
42
|
|
|
{ |
|
43
|
|
|
if (!$this->authService->isGuest()) { |
|
44
|
|
|
return $this->redirectToMain(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$body = $request->getParsedBody(); |
|
48
|
|
|
$error = null; |
|
49
|
|
|
|
|
50
|
|
|
if ($request->getMethod() === Method::POST) { |
|
51
|
|
|
try { |
|
52
|
|
|
foreach (['login', 'password'] as $name) { |
|
53
|
|
|
if (empty($body[$name])) { |
|
54
|
|
|
throw new InvalidArgumentException(ucfirst($name) . ' is required'); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($this->authService->login($body['login'], $body['password'], isset($body['remember']))) { |
|
59
|
|
|
return $this->redirectToMain(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
throw new InvalidArgumentException('Unable to login.'); |
|
63
|
|
|
} catch (Throwable $e) { |
|
64
|
|
|
$this->logger->error($e); |
|
65
|
|
|
$error = $e->getMessage(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->viewRenderer->render('login', [ |
|
70
|
|
|
'body' => $body, |
|
71
|
|
|
'error' => $error, |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function logout(): ResponseInterface |
|
76
|
|
|
{ |
|
77
|
|
|
$this->authService->logout(); |
|
78
|
|
|
|
|
79
|
|
|
return $this->redirectToMain(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function redirectToMain(): ResponseInterface |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->responseFactory->createResponse(Status::FOUND) |
|
85
|
|
|
->withHeader( |
|
86
|
|
|
'Location', |
|
87
|
|
|
$this->urlGenerator->generate('site/index') |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|