186 lines
5.0 KiB
PHP
186 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Laravel\Nova\Nova;
|
|
|
|
class LoginController extends Controller
|
|
{
|
|
use AuthenticatesUsers, ValidatesRequests;
|
|
|
|
/**
|
|
* Username
|
|
*/
|
|
protected string $username = 'username';
|
|
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->middleware('nova.guest:'.config('nova.guard'))->except('logout');
|
|
}
|
|
|
|
/**
|
|
* Show the application's login form.
|
|
*/
|
|
public function showLoginForm()
|
|
{
|
|
return view('vendor.nova.pages.login');
|
|
}
|
|
|
|
/**
|
|
* The user has been authenticated.
|
|
*
|
|
* @param mixed $user
|
|
* @return mixed
|
|
*/
|
|
protected function authenticated(Request $request, $user)
|
|
{
|
|
$redirect = redirect()->intended($this->redirectPath());
|
|
|
|
return $request->wantsJson()
|
|
? new JsonResponse([
|
|
'redirect' => $redirect->getTargetUrl(),
|
|
], 200)
|
|
: $redirect;
|
|
}
|
|
|
|
/**
|
|
* Log the user out of the application.
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function logout(Request $request)
|
|
{
|
|
$this->guard()->logout();
|
|
|
|
$request->session()->invalidate();
|
|
|
|
return redirect()->intended($this->redirectPath());
|
|
}
|
|
|
|
/**
|
|
* Get the post register / login redirect path.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function redirectPath(): string
|
|
{
|
|
return Nova::url(is_callable(Nova::$initialPath) ? call_user_func(Nova::$initialPath) : Nova::$initialPath);
|
|
}
|
|
|
|
/**
|
|
* Get the guard to be used during authentication.
|
|
*
|
|
* @return \Illuminate\Contracts\Auth\StatefulGuard
|
|
*/
|
|
protected function guard()
|
|
{
|
|
return Auth::guard(config('nova.guard'));
|
|
}
|
|
|
|
/**
|
|
* Get the login username to be used by the controller.
|
|
*/
|
|
public function username(): string
|
|
{
|
|
return $this->username;
|
|
}
|
|
|
|
/**
|
|
* Validate the user login request.
|
|
*
|
|
* @return void
|
|
*
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
*/
|
|
protected function validateLogin(Request $request)
|
|
{
|
|
$request->validate([
|
|
$this->username() => ['required', 'string', 'max:250'],
|
|
'password' => ['required', 'string', 'max:250'],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Handle a login request to the application.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
|
|
*
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
*/
|
|
public function login(Request $request)
|
|
{
|
|
$this->validateLogin($request);
|
|
|
|
// If the class is using the ThrottlesLogins trait, we can automatically throttle
|
|
// the login attempts for this application. We'll key this by the username and
|
|
// the IP address of the client making these requests into this application.
|
|
if (method_exists($this, 'hasTooManyLoginAttempts') &&
|
|
$this->hasTooManyLoginAttempts($request)) {
|
|
$this->fireLockoutEvent($request);
|
|
|
|
return $this->sendLockoutResponse($request);
|
|
}
|
|
|
|
$user = User::where('username', $request->username)
|
|
->orWhere('phone', unMaskPhone($request->username))
|
|
->first();
|
|
|
|
if (! $user) {
|
|
return $this->sendFailedLoginResponse($request);
|
|
}
|
|
|
|
if (Hash::check($request->password, $user->password)) {
|
|
auth()->login($user);
|
|
|
|
if ($request->hasSession()) {
|
|
$request->session()->put('auth.password_confirmed_at', time());
|
|
}
|
|
|
|
return $this->sendLoginResponse($request);
|
|
}
|
|
|
|
// If the login attempt was unsuccessful we will increment the number of attempts
|
|
// to login and redirect the user back to the login form. Of course, when this
|
|
// user surpasses their maximum number of attempts they will get locked out.
|
|
$this->incrementLoginAttempts($request);
|
|
|
|
return $this->sendFailedLoginResponse($request);
|
|
}
|
|
|
|
/**
|
|
* Send the response after the user was authenticated.
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
|
*/
|
|
protected function sendLoginResponse(Request $request)
|
|
{
|
|
$request->session()->regenerate();
|
|
|
|
$this->clearLoginAttempts($request);
|
|
|
|
if ($response = $this->authenticated($request, $this->guard()->user())) {
|
|
return $response;
|
|
}
|
|
|
|
return $request->wantsJson()
|
|
? new JsonResponse([
|
|
'url' => $this->redirectPath(),
|
|
], 204)
|
|
: redirect()->intended($this->redirectPath());
|
|
}
|
|
}
|