1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AuthService; |
4
|
|
|
|
5
|
|
|
use AuthService\Contracts\AuthEventListenerInterface; |
6
|
|
|
use AuthService\Contracts\AuthServiceInterface; |
7
|
|
|
use Illuminate\Contracts\Auth\StatefulGuard as StatefulGuardContract; |
8
|
|
|
|
9
|
|
|
class AuthService implements AuthServiceInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Authentication's stateful guard instance. |
13
|
|
|
* |
14
|
|
|
* @var \Illuminate\Contracts\Auth\StatefulGuard |
15
|
|
|
*/ |
16
|
|
|
protected $statefulGuard; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Authentication's event listener instance. |
20
|
|
|
* |
21
|
|
|
* @var \AuthService\Contracts\AuthEventListenerInterface |
22
|
|
|
*/ |
23
|
|
|
protected $eventListener; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new instance of AuthService class. |
27
|
|
|
* |
28
|
|
|
* @param \Illuminate\Contracts\Auth\StatefulGuard $statefulGuard |
29
|
|
|
* @param \AuthService\Contracts\AuthEventListenerInterface $eventListener |
30
|
|
|
*/ |
31
|
5 |
|
public function __construct(StatefulGuardContract $statefulGuard, AuthEventListenerInterface $eventListener) |
32
|
|
|
{ |
33
|
5 |
|
$this->statefulGuard = $statefulGuard; |
34
|
5 |
|
$this->eventListener = $eventListener; |
35
|
5 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get authentication's stateful guard instance. |
39
|
|
|
* |
40
|
|
|
* @return \Illuminate\Contracts\Auth\StatefulGuard |
41
|
|
|
*/ |
42
|
4 |
|
public function statefulGuard() |
43
|
|
|
{ |
44
|
4 |
|
return $this->statefulGuard; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get authentication's event listener instance. |
49
|
|
|
* |
50
|
|
|
* @return \AuthService\Contracts\AuthEventListenerInterface |
51
|
|
|
*/ |
52
|
4 |
|
public function eventListener() |
53
|
|
|
{ |
54
|
4 |
|
return $this->eventListener; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Log the user in. |
59
|
|
|
* |
60
|
|
|
* @param array $credentials |
61
|
|
|
* @param bool $remember |
62
|
|
|
* |
63
|
|
|
* @return \Illuminate\Http\RedirectResponse |
64
|
|
|
*/ |
65
|
2 |
|
public function login(array $credentials, $remember = false) |
66
|
|
|
{ |
67
|
2 |
|
if (! $this->statefulGuard()->attempt($credentials, $remember)) { |
68
|
1 |
|
return $this->eventListener()->userHasFailedToLogIn(); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $this->eventListener()->userHasLoggedIn(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Log the user out. |
76
|
|
|
* |
77
|
|
|
* @return \Illuminate\Http\RedirectResponse |
78
|
|
|
*/ |
79
|
1 |
|
public function logout() |
80
|
|
|
{ |
81
|
1 |
|
$this->statefulGuard()->logout(); |
82
|
|
|
|
83
|
1 |
|
return $this->eventListener()->userHasLoggedOut(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|