Issues (39)

src/Controller/AuthController.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use Psr\Http\Message\ResponseFactoryInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Log\LoggerInterface;
11
use Yiisoft\Auth\IdentityRepositoryInterface;
12
use Yiisoft\Http\Method;
13
use Yiisoft\Router\UrlGeneratorInterface;
14
use Yiisoft\User\CurrentUser;
15
use Yiisoft\Yii\View\ViewRenderer;
16
17
class AuthController
18
{
19
    private ResponseFactoryInterface $responseFactory;
20
    private LoggerInterface $logger;
21
    private UrlGeneratorInterface $urlGenerator;
22
    private ViewRenderer $viewRenderer;
23
    private CurrentUser $currentUser;
24
25
    public function __construct(
26
        ResponseFactoryInterface $responseFactory,
27
        ViewRenderer $viewRenderer,
28
        LoggerInterface $logger,
29
        UrlGeneratorInterface $urlGenerator,
30
        CurrentUser $currentUser
31
    ) {
32
        $this->responseFactory = $responseFactory;
33
        $this->logger = $logger;
34
        $this->urlGenerator = $urlGenerator;
35
        $this->viewRenderer = $viewRenderer->withControllerName('auth');
36
        $this->currentUser = $currentUser;
37
    }
38
39
    public function login(
40
        ServerRequestInterface $request,
41
        IdentityRepositoryInterface $identityRepository
42
    ): ResponseInterface {
43
        $body = $request->getParsedBody();
44
        $error = null;
45
46
        if ($request->getMethod() === Method::POST) {
47
            try {
48
                foreach (['login', 'password'] as $name) {
49
                    if (empty($body[$name])) {
50
                        throw new \InvalidArgumentException(ucfirst($name) . ' is required');
51
                    }
52
                }
53
54
                /** @var \App\User\User $identity */
55
                $identity = $identityRepository->findByLogin($body['login']);
0 ignored issues
show
The method findByLogin() does not exist on Yiisoft\Auth\IdentityRepositoryInterface. It seems like you code against a sub-type of Yiisoft\Auth\IdentityRepositoryInterface such as App\User\UserRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
                /** @scrutinizer ignore-call */ 
56
                $identity = $identityRepository->findByLogin($body['login']);
Loading history...
56
57
                if ($identity === null || !$identity->validatePassword($body['password'])) {
58
                    throw new \InvalidArgumentException('Invalid login or password');
59
                }
60
61
                if ($this->currentUser->login($identity)) {
62
                    return $this->responseFactory
63
                        ->createResponse(302)
64
                        ->withHeader(
65
                            'Location',
66
                            $this->urlGenerator->generate('site/index')
67
                        );
68
                }
69
70
                throw new \InvalidArgumentException('Unable to login');
71
            } catch (\Throwable $e) {
72
                $this->logger->error($e);
73
                $error = $e->getMessage();
74
            }
75
        }
76
77
        return $this->viewRenderer->render(
78
            'login',
79
            [
80
                'body' => $body,
81
                'error' => $error,
82
            ]
83
        );
84
    }
85
86
    public function logout(): ResponseInterface
87
    {
88
        $this->currentUser->logout();
89
90
        return $this->responseFactory
91
            ->createResponse(302)
92
            ->withHeader(
93
                'Location',
94
                $this->urlGenerator->generate('site/index')
95
            );
96
    }
97
}
98