1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cortex\Fort\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Validation\ValidationException; |
7
|
|
|
use Illuminate\Foundation\Auth\ThrottlesLogins; |
8
|
|
|
|
9
|
|
|
trait AuthenticatesUsers |
10
|
|
|
{ |
11
|
|
|
use ThrottlesLogins; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Send the response after the user was authenticated. |
15
|
|
|
* |
16
|
|
|
* @param \Illuminate\Http\Request $request |
17
|
|
|
* |
18
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
19
|
|
|
*/ |
20
|
|
|
protected function sendLoginResponse(Request $request) |
21
|
|
|
{ |
22
|
|
|
$user = auth()->guard($this->getGuard())->user(); |
|
|
|
|
23
|
|
|
|
24
|
|
|
$twofactor = $user->getTwoFactor(); |
25
|
|
|
$totpStatus = $twofactor['totp']['enabled'] ?? false; |
26
|
|
|
$phoneStatus = $twofactor['phone']['enabled'] ?? false; |
27
|
|
|
|
28
|
|
|
$request->session()->regenerate(); |
|
|
|
|
29
|
|
|
$this->clearLoginAttempts($request); |
30
|
|
|
|
31
|
|
|
// Enforce TwoFactor authentication |
32
|
|
|
if ($totpStatus || $phoneStatus) { |
33
|
|
|
$this->processLogout($request); |
34
|
|
|
|
35
|
|
|
$request->session()->put('rinvex.fort.twofactor', ['user_id' => $user->getKey(), 'remember' => $request->filled('remember'), 'totp' => $totpStatus, 'phone' => $phoneStatus]); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$route = $totpStatus |
38
|
|
|
? route('frontarea.verification.phone.verify') |
39
|
|
|
: route('frontarea.verification.phone.request'); |
40
|
|
|
|
41
|
|
|
return intend([ |
42
|
|
|
'url' => $route, |
43
|
|
|
'with' => ['warning' => trans('cortex/fort::messages.verification.twofactor.totp.required')], |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return intend([ |
48
|
|
|
'intended' => route('frontarea.home'), |
49
|
|
|
'with' => ['success' => trans('cortex/fort::messages.auth.login')], |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the failed login response instance. |
55
|
|
|
* |
56
|
|
|
* @param \Illuminate\Http\Request $request |
57
|
|
|
* |
58
|
|
|
* @throws ValidationException |
59
|
|
|
* |
60
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
61
|
|
|
*/ |
62
|
|
|
protected function sendFailedLoginResponse(Request $request) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
throw ValidationException::withMessages([ |
65
|
|
|
$this->username() => [trans('cortex/fort::messages.auth.failed')], |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Redirect the user after determining they are locked out. |
71
|
|
|
* |
72
|
|
|
* @param \Illuminate\Http\Request $request |
73
|
|
|
* |
74
|
|
|
* @throws \Illuminate\Validation\ValidationException |
75
|
|
|
* |
76
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
77
|
|
|
*/ |
78
|
|
|
protected function sendLockoutResponse(Request $request) |
79
|
|
|
{ |
80
|
|
|
$seconds = $this->limiter()->availableIn( |
81
|
|
|
$this->throttleKey($request) |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
throw ValidationException::withMessages([ |
85
|
|
|
$this->username() => [trans('cortex/fort::messages.auth.lockout', ['seconds' => $seconds])], |
86
|
|
|
])->status(423); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Process logout. |
91
|
|
|
* |
92
|
|
|
* @param \Illuminate\Http\Request $request |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
protected function processLogout(Request $request): void |
97
|
|
|
{ |
98
|
|
|
auth()->guard($this->getGuard())->logout(); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$request->session()->invalidate(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the login username to be used by the controller. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function username() |
109
|
|
|
{ |
110
|
|
|
return 'loginfield'; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.