Issues (6)

src/JWTProxy.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Signifly\Janitor;
4
5
use Illuminate\Auth\Events\Attempting;
6
use Illuminate\Auth\Events\Authenticated;
7
use Illuminate\Auth\Events\Failed;
8
use Illuminate\Auth\Events\Login;
9
use Illuminate\Auth\Events\Logout;
10
use Illuminate\Support\Facades\Auth;
11
use Signifly\Janitor\Exceptions\InvalidCredentialsException;
12
13
class JWTProxy extends AbstractProxy
14
{
15
    /**
16
     * Attempt to log the user in by username and password.
17
     *
18
     * @param  string $username
19
     * @param  mixed $password
20
     * @return array
21
     */
22
    public function attemptLogin($username, $password): array
23
    {
24
        $credentials = [
25
            $this->getUsernameField() => $username,
26
            'password' => $password,
27
        ];
28
29
        event(new Attempting($this->getGuard(), $credentials, false));
30
31
        $user = $this->getUserProvider()
32
            ->retrieveByCredentials($credentials);
33
34
        $token = Auth::attempt($credentials);
35
36
        if (is_null($user) || ! $token) {
37
            event(new Failed($this->getGuard(), $user, $credentials));
38
            throw InvalidCredentialsException::forUsername($username);
39
        }
40
41
        event(new Authenticated($this->getGuard(), $user));
42
        event(new Login($this->getGuard(), $user, false));
43
44
        return $this->prepareToken($token);
0 ignored issues
show
$token of type true is incompatible with the type string expected by parameter $token of Signifly\Janitor\JWTProxy::prepareToken(). ( Ignorable by Annotation )

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

44
        return $this->prepareToken(/** @scrutinizer ignore-type */ $token);
Loading history...
45
    }
46
47
    /**
48
     * Attempt refreshing the token.
49
     *
50
     * @param  string|null $refreshToken
51
     * @return array
52
     */
53
    public function attemptRefresh($refreshToken = null): array
54
    {
55
        $token = Auth::refresh();
56
57
        return $this->prepareToken($token);
58
    }
59
60
    /**
61
     * Attempt to log the user out.
62
     *
63
     * @return void
64
     */
65
    public function attemptLogout(): void
66
    {
67
        $user = Auth::user();
68
69
        Auth::logout();
70
71
        event(new Logout($this->getGuard(), $user));
72
    }
73
74
    /**
75
     * Prepare the token.
76
     *
77
     * @param  string $token
78
     * @return array
79
     */
80
    protected function prepareToken($token): array
81
    {
82
        return [
83
            'access_token' => $token,
84
            'token_type' => 'Bearer',
85
            'expires_in' => Auth::factory()->getTTL() * 60,
86
        ];
87
    }
88
}
89