LoginController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Http\Controllers\Teacher\Auth;
  3. use App\Models\Teacher;
  4. use App\Http\Controllers\Teacher\Controller;
  5. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Validator;
  9. class LoginController extends Controller
  10. {
  11. /*
  12. |--------------------------------------------------------------------------
  13. | Login Controller
  14. |--------------------------------------------------------------------------
  15. |
  16. | This controller handles authenticating users for the application and
  17. | redirecting them to your home screen. The controller uses a trait
  18. | to conveniently provide its functionality to your applications.
  19. |
  20. */
  21. use AuthenticatesUsers;
  22. /**
  23. * Where to redirect users after login.
  24. *
  25. * @var string
  26. */
  27. protected $redirectTo = '/teacher';
  28. /**
  29. * 重写登录视图页面
  30. * @author 晚黎
  31. * @date 2016-09-05T23:06:16+0800
  32. * @return [type] [description]
  33. */
  34. public function showLoginForm()
  35. {
  36. return view('teacher.auth.login');
  37. }
  38. /**
  39. * 自定义认证驱动
  40. * @author 晚黎
  41. * @date 2016-09-05T23:53:07+0800
  42. * @return [type] [description]
  43. */
  44. protected function guard()
  45. {
  46. return auth()->guard('teacher');
  47. }
  48. public function username()
  49. {
  50. return 'phone';
  51. }
  52. public function login(Request $request)
  53. {
  54. $validator = Validator::make($request->all(), [
  55. $this->username() => 'required|string',
  56. 'password' => 'required|string'
  57. ]);
  58. if($validator->fails()) {
  59. return back()->withErrors($validator);
  60. }
  61. // If the class is using the ThrottlesLogins trait, we can automatically throttle
  62. // the login attempts for this application. We'll key this by the username and
  63. // the IP address of the client making these requests into this application.
  64. if ($this->hasTooManyLoginAttempts($request)) {
  65. $this->fireLockoutEvent($request);
  66. return $this->sendLockoutResponse($request);
  67. }
  68. if ($this->attemptLogin($request)) {
  69. return $this->sendLoginResponse($request);
  70. }
  71. // If the login attempt was unsuccessful we will increment the number of attempts
  72. // to login and redirect the user back to the login form. Of course, when this
  73. // user surpasses their maximum number of attempts they will get locked out.
  74. $this->incrementLoginAttempts($request);
  75. return $this->showWarning('密码或者手机错误', '/teacher/login');
  76. // return $this->sendFailedLoginResponse($request);
  77. }
  78. public function logout()
  79. {
  80. Auth::guard('teacher')->logout();
  81. return redirect('/teacher/login');
  82. }
  83. public function showChangePasswordForm(Request $request)
  84. {
  85. return view('teacher.auth.change-password');
  86. }
  87. public function changePassword(Request $request)
  88. {
  89. $teacher = Auth::guard('teacher')->user();
  90. if(!empty($request->input('password'))) {
  91. $teacher->password = bcrypt($request->input('password'));
  92. $teacher->save();
  93. }
  94. return $this->showMessage('操作成功');
  95. }
  96. }