AuthUserTrait.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace App\Http\Controllers\Traits;
  3. use Illuminate\Foundation\Auth\RedirectsUsers;
  4. use Illuminate\Foundation\Auth\ThrottlesLogins;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Auth;
  7. trait AuthUserTrait
  8. {
  9. use RedirectsUsers, ThrottlesLogins;
  10. /**
  11. * Show the application's login form.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function showLoginForm()
  16. {
  17. config()->set(['admin'=>['title'=>'后台登陆']]);
  18. return view('auth.login');
  19. }
  20. /**
  21. * Handle a login request to the application.
  22. *
  23. * @param \Illuminate\Http\Request $request
  24. * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
  25. */
  26. public function login(Request $request)
  27. {
  28. $this->validateLogin($request);
  29. // 验证码验证
  30. $captcha = captcha_check($request->input('captcha'));
  31. // 验证失败
  32. if( !$captcha ){
  33. return redirect()->back()
  34. ->withInput($request->only($this->username(), 'remember'))
  35. ->withErrors(['captcha'=>'验证错误']);
  36. }
  37. // If the class is using the ThrottlesLogins trait, we can automatically throttle
  38. // the login attempts for this application. We'll key this by the username and
  39. // the IP address of the client making these requests into this application.
  40. if ($this->hasTooManyLoginAttempts($request)) {
  41. $this->fireLockoutEvent($request);
  42. return $this->sendLockoutResponse($request);
  43. }
  44. if ($this->attemptLogin($request)) {
  45. // $request->setTrustedProxies(array('10.32.0.1/16'));
  46. // $ip = $request->getClientIp();
  47. // // 记录登录信息
  48. // file_put_contents(
  49. // storage_path('logs/auth-login.txt'),
  50. // "登录时间:".(date('Y-m-d H:i:s'))." 登录IP:{$ip}"."\r\n", FILE_APPEND
  51. // );
  52. return $this->sendLoginResponse($request);
  53. }
  54. // If the login attempt was unsuccessful we will increment the number of attempts
  55. // to login and redirect the user back to the login form. Of course, when this
  56. // user surpasses their maximum number of attempts they will get locked out.
  57. $this->incrementLoginAttempts($request);
  58. return $this->sendFailedLoginResponse($request);
  59. }
  60. /**
  61. * Validate the user login request.
  62. *
  63. * @param \Illuminate\Http\Request $request
  64. * @return void
  65. */
  66. protected function validateLogin(Request $request)
  67. {
  68. $this->validate($request, [
  69. $this->username() => 'required|string',
  70. 'password' => 'required|string',
  71. 'captcha' => 'required|string',
  72. ],[
  73. 'required' => '必填'
  74. // $this->username().'.required' => '必填',
  75. // 'password.required' => '必填',
  76. // 'captcha.required' => '必填'
  77. ]);
  78. }
  79. /**
  80. * Attempt to log the user into the application.
  81. *
  82. * @param \Illuminate\Http\Request $request
  83. * @return bool
  84. */
  85. protected function attemptLogin(Request $request)
  86. {
  87. return $this->guard()->attempt(
  88. $this->credentials($request), $request->has('remember')
  89. );
  90. }
  91. /**
  92. * Get the needed authorization credentials from the request.
  93. *
  94. * @param \Illuminate\Http\Request $request
  95. * @return array
  96. */
  97. protected function credentials(Request $request)
  98. {
  99. return $request->only($this->username(), 'password');
  100. }
  101. /**
  102. * Send the response after the user was authenticated.
  103. *
  104. * @param \Illuminate\Http\Request $request
  105. * @return \Illuminate\Http\Response
  106. */
  107. protected function sendLoginResponse(Request $request)
  108. {
  109. $request->session()->regenerate();
  110. $this->clearLoginAttempts($request);
  111. return $this->authenticated($request, $this->guard()->user())
  112. ?: redirect()->intended($this->redirectPath());
  113. }
  114. /**
  115. * The user has been authenticated.
  116. *
  117. * @param \Illuminate\Http\Request $request
  118. * @param mixed $user
  119. * @return mixed
  120. */
  121. protected function authenticated(Request $request, $user)
  122. {
  123. //
  124. }
  125. /**
  126. * Get the failed login response instance.
  127. *
  128. * @param \Illuminate\Http\Request $request
  129. * @return \Illuminate\Http\RedirectResponse
  130. */
  131. protected function sendFailedLoginResponse(Request $request)
  132. {
  133. // $errors = [$this->username() => trans('auth.failed')];
  134. $errors = [$this->username() => '账号或密码错误'];;
  135. if ($request->expectsJson()) {
  136. return response()->json($errors, 422);
  137. }
  138. return redirect()->back()
  139. ->withInput($request->only($this->username(), 'remember'))
  140. ->withErrors($errors);
  141. }
  142. /**
  143. * Get the login username to be used by the controller.
  144. *
  145. * @return string
  146. */
  147. public function username()
  148. {
  149. return 'email';
  150. }
  151. /**
  152. * Log the user out of the application.
  153. *
  154. * @param \Illuminate\Http\Request $request
  155. * @return \Illuminate\Http\Response
  156. */
  157. public function logout(Request $request)
  158. {
  159. $this->guard()->logout();
  160. $request->session()->flush();
  161. $request->session()->regenerate();
  162. return redirect('/admin/login');
  163. }
  164. /**
  165. * Get the guard to be used during authentication.
  166. *
  167. * @return \Illuminate\Contracts\Auth\StatefulGuard
  168. */
  169. protected function guard()
  170. {
  171. return Auth::guard();
  172. }
  173. }