Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
LoginEndpoint
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace Olz\Api\Endpoints;
4
5use Olz\Api\OlzTypedEndpoint;
6use Olz\Exceptions\AuthBlockedException;
7use Olz\Exceptions\InvalidCredentialsException;
8
9/**
10 * @extends OlzTypedEndpoint<
11 *   array{
12 *     usernameOrEmail: non-empty-string,
13 *     password: non-empty-string,
14 *     rememberMe: bool,
15 *   },
16 *   array{
17 *     status: 'AUTHENTICATED'|'INVALID_CREDENTIALS'|'BLOCKED',
18 *     numRemainingAttempts: ?int<0, max>,
19 *   }
20 * >
21 */
22class LoginEndpoint extends OlzTypedEndpoint {
23    protected function handle(mixed $input): mixed {
24        $username_or_email = trim($input['usernameOrEmail']);
25        $password = $input['password'];
26        $remember_me = $input['rememberMe'];
27
28        try {
29            $user = $this->authUtils()->authenticate($username_or_email, $password);
30        } catch (AuthBlockedException $exc) {
31            return [
32                'status' => 'BLOCKED',
33                'numRemainingAttempts' => 0,
34            ];
35        } catch (InvalidCredentialsException $exc) {
36            return [
37                'status' => 'INVALID_CREDENTIALS',
38                'numRemainingAttempts' => $exc->getNumRemainingAttempts(),
39            ];
40        }
41
42        $now_datetime = new \DateTime($this->dateUtils()->getIsoNow());
43        $user->setLastLoginAt($now_datetime);
44        $this->entityManager()->flush();
45
46        $this->session()->resetConfigure([
47            'timeout' => $remember_me ? 2419200 : 3600, // a month / an hour
48        ]);
49
50        $this->authUtils()->setSessionUser($user);
51        $this->authUtils()->setSessionAuthUser($user);
52        return [
53            'status' => 'AUTHENTICATED',
54            'numRemainingAttempts' => null,
55        ];
56    }
57}