middleware('guest')->except('logout'); $this->username = $this->findUsername(); } public function login(Request $request) { $user = null; if (!$user) { $msg = 'Authenticating user (' . $request->username . ') against database.'; activityLogIt(__CLASS__, __FUNCTION__, 'info', $msg, 'authentication'); $this->validateLogin($request); // replaced $this->validateLogin in AuthenticatesUsers.php with private version in this class if ($this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } if ($this->attemptLogin($request)) { if ($user = Auth::user()) { $msg = 'Local authentication for user ' . $user->email; activityLogIt(__CLASS__, __FUNCTION__, 'info', $msg, 'authentication'); $user->last_login = Carbon::now(); $user->save(); return redirect('/dashboard'); } return $this->sendLoginResponse($request); } $this->incrementLoginAttempts($request); $msg = 'Local authentication failed.'; activityLogIt(__CLASS__, __FUNCTION__, 'error', $msg, 'authentication'); return $this->sendFailedLoginResponse($request); } } public function showLoginForm() { $banner = \App\Models\Banner::select('login_banner')->get(); $login_banner = $banner[0]->login_banner; return view('auth.login', compact('login_banner')); } public function showLoggedOut() { // assume we're logged out at this point return view('auth.logged-out'); } public function logout(Request $request) { $this->guard()->logout(); $request->session()->flush(); $request->session()->regenerate(); return redirect('/login'); } /** * Get the login username to be used by the controller. * * @return string */ public function findUsername() { $login = request()->input('username'); $fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username'; request()->merge([$fieldType => $login]); return $fieldType; } /** * Get username property. * * @return string */ public function username() { return $this->username; } /** * Validate the user login request. * * @param \Illuminate\Http\Request $request * @return void * * @throws \Illuminate\Validation\ValidationException */ private function validateLogin(Request $request) { $request->validate([ 'username' => 'required|string', 'password' => 'required|string', ]); } /** * Redirect the user after determining they are locked out. * * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response * * @throws \Illuminate\Validation\ValidationException */ private function sendLockoutResponse(Request $request) { $seconds = $this->limiter()->availableIn( $this->throttleKey($request) ); throw ValidationException::withMessages([ 'username' => [trans('auth.throttle', [ 'seconds' => $seconds, 'minutes' => ceil($seconds / 60), ])], ])->status(Response::HTTP_TOO_MANY_REQUESTS); } /** * Get the failed login response instance. * * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response * * @throws \Illuminate\Validation\ValidationException */ protected function sendFailedLoginResponse(Request $request) { throw ValidationException::withMessages([ 'username' => [trans('auth.failed')], ]); } }