1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Fort Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Fort Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Rinvex\Fort\Http\Controllers\Frontend; |
17
|
|
|
|
18
|
|
|
use Illuminate\Http\Request; |
19
|
|
|
use Illuminate\Support\Facades\Auth; |
20
|
|
|
use Illuminate\Support\Facades\Lang; |
21
|
|
|
use Rinvex\Fort\Guards\SessionGuard; |
22
|
|
|
use Rinvex\Fort\Http\Requests\UserAuthentication; |
23
|
|
|
use Rinvex\Fort\Http\Controllers\AbstractController; |
24
|
|
|
|
25
|
|
|
class AuthenticationController extends AbstractController |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
protected $middlewareWhitelist = ['logout']; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a new authentication controller instance. |
34
|
|
|
* |
35
|
|
|
* @return void |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function __construct() |
38
|
|
|
{ |
39
|
|
|
$this->middleware($this->getGuestMiddleware(), ['except' => $this->middlewareWhitelist]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Show the login form. |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
public function showLogin() |
48
|
|
|
{ |
49
|
|
|
return view('rinvex.fort::authentication.login'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Process to the login form. |
54
|
|
|
* |
55
|
|
|
* @param \Rinvex\Fort\Http\Requests\UserAuthentication $request |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
58
|
|
|
*/ |
59
|
|
|
public function processLogin(UserAuthentication $request) |
60
|
|
|
{ |
61
|
|
|
// Prepare variables |
62
|
|
|
$remember = $request->has('remember'); |
63
|
|
|
$loginField = get_login_field($request->get('loginfield')); |
64
|
|
|
$credentials = [ |
65
|
|
|
$loginField => $request->input('loginfield'), |
66
|
|
|
'password' => $request->input('password'), |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
$result = Auth::guard($this->getGuard())->attempt($credentials, $remember); |
70
|
|
|
|
71
|
|
|
return $this->getLoginResponse($request, $result); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Logout currently logged in user. |
76
|
|
|
* |
77
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
78
|
|
|
*/ |
79
|
|
|
public function logout() |
80
|
|
|
{ |
81
|
|
|
$result = Auth::guard($this->getGuard())->logout(); |
82
|
|
|
|
83
|
|
|
return intend([ |
84
|
|
|
'intended' => route('home'), |
85
|
|
|
'with' => ['rinvex.fort.alert.warning' => Lang::get($result)], |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get login response upon the given request & result. |
91
|
|
|
* |
92
|
|
|
* @param \Illuminate\Http\Request $request |
93
|
|
|
* @param string $result |
94
|
|
|
* |
95
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
96
|
|
|
*/ |
97
|
|
|
protected function getLoginResponse(Request $request, $result) |
98
|
|
|
{ |
99
|
|
|
switch ($result) { |
100
|
|
|
// Too many failed logins, user locked out |
101
|
|
|
case SessionGuard::AUTH_LOCKED_OUT: |
102
|
|
|
$seconds = Auth::guard($this->getGuard())->secondsRemainingOnLockout($request); |
103
|
|
|
|
104
|
|
|
return intend([ |
105
|
|
|
'intended' => route('home'), |
106
|
|
|
'withInput' => $request->only('loginfield', 'remember'), |
107
|
|
|
'withErrors' => ['loginfield' => Lang::get($result, ['seconds' => $seconds])], |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
// Valid credentials, but user is unverified; Can NOT login! |
111
|
|
|
case SessionGuard::AUTH_UNVERIFIED: |
112
|
|
|
return intend([ |
113
|
|
|
'intended' => route('rinvex.fort.verification.email'), |
114
|
|
|
'withErrors' => ['email' => Lang::get($result)], |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
// Valid credentials, but user is moderated; Can NOT login! |
118
|
|
|
case SessionGuard::AUTH_MODERATED: |
119
|
|
|
return intend([ |
120
|
|
|
'home' => true, |
121
|
|
|
'withErrors' => ['rinvex.fort.auth.moderated' => Lang::get($result)], |
122
|
|
|
]); |
123
|
|
|
|
124
|
|
|
// Wrong credentials, failed login |
125
|
|
|
case SessionGuard::AUTH_FAILED: |
126
|
|
|
return intend([ |
127
|
|
|
'back' => true, |
128
|
|
|
'withInput' => $request->only('loginfield', 'remember'), |
129
|
|
|
'withErrors' => ['loginfield' => Lang::get($result)], |
130
|
|
|
]); |
131
|
|
|
|
132
|
|
|
// Two-Factor authentication required |
133
|
|
|
case SessionGuard::AUTH_TWOFACTOR_REQUIRED: |
134
|
|
|
$route = ! isset(session('rinvex.fort.twofactor.methods')['totp']) ? 'rinvex.fort.verification.phone' : 'rinvex.fort.verification.phone.verify'; |
|
|
|
|
135
|
|
|
|
136
|
|
|
return intend([ |
137
|
|
|
'route' => $route, |
138
|
|
|
'with' => ['rinvex.fort.alert.warning' => Lang::get($result)], |
139
|
|
|
]); |
140
|
|
|
|
141
|
|
|
// Login successful and everything is fine! |
142
|
|
|
case SessionGuard::AUTH_LOGIN: |
143
|
|
|
default: |
144
|
|
|
return intend([ |
145
|
|
|
'intended' => route('home'), |
146
|
|
|
'with' => ['rinvex.fort.alert.success' => Lang::get($result)], |
147
|
|
|
]); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.