LoginController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\User;
  5. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  6. use Illuminate\Http\Request;
  7. class LoginController extends Controller
  8. {
  9. /*
  10. |--------------------------------------------------------------------------
  11. | Login Controller
  12. |--------------------------------------------------------------------------
  13. |
  14. | This controller handles authenticating users for the application and
  15. | redirecting them to your home screen. The controller uses a trait
  16. | to conveniently provide its functionality to your applications.
  17. |
  18. */
  19. use AuthenticatesUsers {
  20. logout as traitLogout;
  21. }
  22. /**
  23. * Where to redirect users after login.
  24. *
  25. * @var string
  26. */
  27. protected string $redirectTo = '/order';
  28. /**
  29. * Create a new controller instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. $this->middleware('guest')->except('logout');
  36. $this->middleware('auth')->only('logout');
  37. }
  38. public function login(Request $request)
  39. {
  40. $this->validateLogin($request);
  41. // If the class is using the ThrottlesLogins trait, we can automatically throttle
  42. // the login attempts for this application. We'll key this by the username and
  43. // the IP address of the client making these requests into this application.
  44. if (method_exists($this, 'hasTooManyLoginAttempts') &&
  45. $this->hasTooManyLoginAttempts($request)) {
  46. $this->fireLockoutEvent($request);
  47. return $this->sendLockoutResponse($request);
  48. }
  49. if ($this->attemptLogin($request)) {
  50. if ($request->hasSession()) {
  51. $request->session()->put('auth.password_confirmed_at', time());
  52. }
  53. if ($request->session()->has('token_fcm') && auth()->id()) {
  54. $token = trim((string)$request->session()->get('token_fcm'));
  55. if ($token !== '') {
  56. User::assignUniqueFcmToken((int)auth()->id(), $token);
  57. }
  58. }
  59. return $this->sendLoginResponse($request);
  60. }
  61. // If the login attempt was unsuccessful we will increment the number of attempts
  62. // to login and redirect the user back to the login form. Of course, when this
  63. // user surpasses their maximum number of attempts they will get locked out.
  64. $this->incrementLoginAttempts($request);
  65. return $this->sendFailedLoginResponse($request);
  66. }
  67. public function logout(Request $request)
  68. {
  69. if ($request->user()) {
  70. User::clearFcmToken((int)$request->user()->id);
  71. }
  72. return $this->traitLogout($request);
  73. }
  74. }