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 Rinvex\Fort\Guards\SessionGuard; |
21
|
|
|
use Rinvex\Fort\Http\Controllers\AbstractController; |
22
|
|
|
use Rinvex\Fort\Http\Requests\Frontend\UserAuthenticationRequest; |
23
|
|
|
|
24
|
|
|
class AuthenticationController extends AbstractController |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
protected $middlewareWhitelist = ['logout']; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new authentication controller instance. |
33
|
|
|
*/ |
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
$this->middleware($this->getGuestMiddleware(), ['except' => $this->middlewareWhitelist]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Show the login form. |
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function form() |
45
|
|
|
{ |
46
|
|
|
// Remember previous URL for later redirect back |
47
|
|
|
session()->put('url.intended', url()->previous()); |
48
|
|
|
|
49
|
|
|
return view('rinvex/fort::frontend/authentication.login'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Process to the login form. |
54
|
|
|
* |
55
|
|
|
* @param \Rinvex\Fort\Http\Requests\Frontend\UserAuthenticationRequest $request |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
58
|
|
|
*/ |
59
|
|
|
public function login(UserAuthenticationRequest $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
|
|
|
'url' => '/', |
85
|
|
|
'with' => ['warning' => trans($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
|
|
|
'url' => '/', |
106
|
|
|
'withInput' => $request->only('loginfield', 'remember'), |
107
|
|
|
'withErrors' => ['loginfield' => trans($result, ['seconds' => $seconds])], |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
// Valid credentials, but user is unverified; Can NOT login! |
111
|
|
|
case SessionGuard::AUTH_UNVERIFIED: |
112
|
|
|
return intend([ |
113
|
|
|
'route' => 'rinvex.fort.frontend.verification.email.request', |
114
|
|
|
'withErrors' => ['email' => trans($result)], |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
// Wrong credentials, failed login |
118
|
|
|
case SessionGuard::AUTH_FAILED: |
119
|
|
|
return intend([ |
120
|
|
|
'back' => true, |
121
|
|
|
'withInput' => $request->only('loginfield', 'remember'), |
122
|
|
|
'withErrors' => ['loginfield' => trans($result)], |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
// Two-Factor authentication required |
126
|
|
|
case SessionGuard::AUTH_TWOFACTOR_REQUIRED: |
127
|
|
|
$route = ! isset(session('rinvex.fort.twofactor.methods')['totp']) ? 'rinvex.fort.frontend.verification.phone.request' : 'rinvex.fort.frontend.verification.phone.verify'; |
|
|
|
|
128
|
|
|
|
129
|
|
|
return intend([ |
130
|
|
|
'route' => $route, |
131
|
|
|
'with' => ['warning' => trans($result)], |
132
|
|
|
]); |
133
|
|
|
|
134
|
|
|
// Login successful and everything is fine! |
135
|
|
|
case SessionGuard::AUTH_LOGIN: |
136
|
|
|
default: |
137
|
|
|
return intend([ |
138
|
|
|
'intended' => url('/'), |
139
|
|
|
'with' => ['success' => trans($result)], |
140
|
|
|
]); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.