Passed
Push — master ( 437590...f4c717 )
by Morten Poul
02:12
created

JWTProxy::prepareToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Signifly\Janitor;
4
5
use Illuminate\Auth\Events\Login;
6
use Illuminate\Auth\Events\Failed;
7
use Illuminate\Auth\Events\Logout;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Auth\Events\Attempting;
10
use Illuminate\Auth\Events\Authenticated;
11
use Signifly\Janitor\Exceptions\InvalidCredentialsException;
12
13
class JWTProxy extends AbstractProxy
14
{
15
    public function attemptLogin($username, $password)
16
    {
17
        $credentials = [
18
            $this->getUsernameField() => $username,
19
            'password' => $password,
20
        ];
21
22
        event(new Attempting($this->getGuard(), $credentials, false));
23
24
        $user = $this->getUserInstance()
25
            ->where($this->getUsernameField(), $username)
26
            ->first();
27
28
        $token = Auth::attempt($credentials);
29
30
        if (is_null($user) || is_null($token)) {
31
            event(new Failed($this->getGuard(), $user, $credentials));
32
            throw InvalidCredentialsException::forUsername($username);
33
        }
34
35
        event(new Authenticated($this->getGuard(), $user));
36
        event(new Login($this->getGuard(), $user, false));
37
38
        return $this->prepareToken($token);
39
    }
40
41
    public function attemptRefresh($refreshToken = null)
42
    {
43
        $token = Auth::refresh();
44
45
        return $this->prepareToken($token);
46
    }
47
48
    public function attemptLogout()
49
    {
50
        $user = Auth::user();
51
52
        Auth::logout();
53
54
        event(new Logout($this->getGuard(), $user));
55
    }
56
57
    protected function prepareToken($token): array
58
    {
59
        return [
60
            'access_token' => $token,
61
            'token_type' => 'Bearer',
62
            'expires_in' => Auth::factory()->getTTL() * 60,
63
        ];
64
    }
65
}
66