|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Auth; |
|
4
|
|
|
|
|
5
|
|
|
use App\Events\UserCreationRequestSent; |
|
6
|
|
|
use App\Http\Requests\LoginUserRequest; |
|
7
|
|
|
use App\Http\Requests\RegisterUserRequest; |
|
8
|
|
|
use App\Orders\CreateWalletOrder; |
|
9
|
|
|
use App\Repositories\UserRepository; |
|
10
|
|
|
use App\Repositories\WalletRepository; |
|
11
|
|
|
use Auth; |
|
12
|
|
|
use Validator; |
|
13
|
|
|
use Illuminate\Http\Request; |
|
14
|
|
|
use App\Http\Controllers\Controller; |
|
15
|
|
|
use Illuminate\Events\Dispatcher; |
|
16
|
|
|
use Illuminate\Foundation\Auth\ThrottlesLogins; |
|
17
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; |
|
18
|
|
|
|
|
19
|
|
|
class AuthController extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
/* |
|
22
|
|
|
|-------------------------------------------------------------------------- |
|
23
|
|
|
| Registration & Login Controller |
|
24
|
|
|
|-------------------------------------------------------------------------- |
|
25
|
|
|
| |
|
26
|
|
|
| This controller handles the registration of new users, as well as the |
|
27
|
|
|
| authentication of existing users. By default, this controller uses |
|
28
|
|
|
| a simple trait to add these behaviors. Why don't you explore it? |
|
29
|
|
|
| |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
use AuthenticatesAndRegistersUsers, ThrottlesLogins; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Where to redirect users after login / registration. |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $redirectTo = '/'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var UserRepository |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $users; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var WalletRepository |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $wallets; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Login by email. |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
public $username = 'email'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* AuthController constructor. |
|
60
|
|
|
* |
|
61
|
|
|
* @param UserRepository $userRepository |
|
62
|
|
|
* @param WalletRepository $walletRepository |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct( |
|
65
|
|
|
UserRepository $userRepository = null, |
|
66
|
|
|
WalletRepository $walletRepository = null |
|
67
|
|
|
) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->middleware($this->guestMiddleware(), ['except' => 'logout']); |
|
70
|
|
|
$this->users = $userRepository; |
|
71
|
|
|
$this->wallets = $walletRepository; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get recover password page. |
|
76
|
|
|
* |
|
77
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getRecover() |
|
80
|
|
|
{ |
|
81
|
|
|
return view('auth.recover'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function modalLogin(Request $request) |
|
85
|
|
|
{ |
|
86
|
|
|
$validation = Validator::make($request->all(), [ |
|
87
|
|
|
$this->loginUsername() => 'required', |
|
88
|
|
|
'password' => 'required' |
|
89
|
|
|
]); |
|
90
|
|
|
|
|
91
|
|
|
$throttles = $this->isUsingThrottlesLoginsTrait(); |
|
92
|
|
|
|
|
93
|
|
|
if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) { |
|
94
|
|
|
$this->fireLockoutEvent($request); |
|
95
|
|
|
|
|
96
|
|
|
return json_encode(['errors' => [ |
|
97
|
|
|
$this->loginUsername() => [$this->getLockoutErrorMessage($this->secondsRemainingOnLockout($request))] |
|
98
|
|
|
]]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if($validation->fails()) { |
|
102
|
|
|
if ($throttles && ! $lockedOut) |
|
|
|
|
|
|
103
|
|
|
$this->incrementLoginAttempts($request); |
|
104
|
|
|
|
|
105
|
|
|
return json_encode(['errors' => $validation->errors()]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$credentials = $this->getCredentials($request); |
|
109
|
|
|
|
|
110
|
|
View Code Duplication |
if(Auth::validate($credentials)) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
if(! $this->users->getByEmail($credentials['email'])->active) |
|
113
|
|
|
return json_encode(['errors' => ['This account has blocked. Please contact the support.']]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) { |
|
117
|
|
|
|
|
118
|
|
|
if ($throttles) { |
|
119
|
|
|
$this->clearLoginAttempts($request); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$user = Auth::user(); |
|
123
|
|
|
|
|
124
|
|
|
(new CreateWalletOrder($user)); |
|
125
|
|
|
|
|
126
|
|
|
if(! $user->confirmed) |
|
127
|
|
|
return json_encode(['redirect' => route('resend_verify_email_form')]); |
|
128
|
|
|
|
|
129
|
|
|
return json_encode([]); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function modalRegister(Request $request, Dispatcher $events) |
|
134
|
|
|
{ |
|
135
|
|
|
$validation = Validator::make( |
|
136
|
|
|
$request->all(), RegisterUserRequest::getRules() |
|
137
|
|
|
); |
|
138
|
|
|
if($validation->fails()) |
|
139
|
|
|
return json_encode(['errors' => $validation->errors()]); |
|
140
|
|
|
|
|
141
|
|
|
$user = $this->users->createSimpleUser($request->all()); |
|
142
|
|
|
|
|
143
|
|
|
$events->fire(new UserCreationRequestSent($user)); |
|
144
|
|
|
|
|
145
|
|
|
Auth::guard($this->getGuard())->login($user); |
|
146
|
|
|
|
|
147
|
|
|
return json_encode(['redirect' => route('resend_verify_email_form')]); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param LoginUserRequest $request |
|
152
|
|
|
* @param Dispatcher $events |
|
153
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response |
|
154
|
|
|
*/ |
|
155
|
|
|
public function postLogin(LoginUserRequest $request, Dispatcher $events) |
|
|
|
|
|
|
156
|
|
|
{ |
|
157
|
|
|
$throttles = $this->isUsingThrottlesLoginsTrait(); |
|
158
|
|
|
|
|
159
|
|
|
if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) { |
|
160
|
|
|
$this->fireLockoutEvent($request); |
|
161
|
|
|
|
|
162
|
|
|
return $this->sendLockoutResponse($request); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$credentials = $this->getCredentials($request); |
|
166
|
|
|
|
|
167
|
|
View Code Duplication |
if(Auth::validate($credentials)) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
if(! $this->users->getByEmail($credentials['email'])->active) |
|
170
|
|
|
return redirect()->back() |
|
171
|
|
|
->withErrors(['account' => 'This account has blocked. Please contact the support.']); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) { |
|
175
|
|
|
|
|
176
|
|
|
if ($throttles) { |
|
177
|
|
|
$this->clearLoginAttempts($request); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if (method_exists($this, 'authenticated')) { |
|
181
|
|
|
return $this->authenticated($request, Auth::guard($this->getGuard())->user()); |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
// todo: HIGH. Fix it!!! |
|
|
|
|
|
|
185
|
|
|
// if(session()->pull('url.intended', '/') == route('admin_login')) |
|
186
|
|
|
// return redirect()->to('/'); |
|
187
|
|
|
|
|
188
|
|
|
$user = Auth::user(); |
|
189
|
|
|
|
|
190
|
|
|
(new CreateWalletOrder($user)); |
|
191
|
|
|
|
|
192
|
|
|
if(! $user->confirmed) |
|
193
|
|
|
return redirect()->route('resend_verify_email_form'); |
|
194
|
|
|
|
|
195
|
|
|
return redirect()->intended($this->redirectPath()); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
if ($throttles && ! $lockedOut) { |
|
|
|
|
|
|
199
|
|
|
$this->incrementLoginAttempts($request); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $this->sendFailedLoginResponse($request); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Post register. |
|
207
|
|
|
* |
|
208
|
|
|
* @param RegisterUserRequest $request |
|
209
|
|
|
* @param Dispatcher $events |
|
210
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
211
|
|
|
*/ |
|
212
|
|
|
public function postRegister(RegisterUserRequest $request, Dispatcher $events) |
|
213
|
|
|
{ |
|
214
|
|
|
$user = $this->users->createSimpleUser($request->all()); |
|
215
|
|
|
|
|
216
|
|
|
$events->fire(new UserCreationRequestSent($user)); |
|
217
|
|
|
|
|
218
|
|
|
Auth::guard($this->getGuard())->login($user); |
|
219
|
|
|
|
|
220
|
|
|
return redirect()->route('resend_verify_email_form'); |
|
221
|
|
|
} |
|
222
|
|
|
} |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: