LoginController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 (auth()->id()) {
  54. $tokenFromRequest = trim((string)$request->input('token_fcm', ''));
  55. $tokenFromSession = trim((string)$request->session()->get('token_fcm', ''));
  56. $tokenFromCookie = trim((string)$request->cookie('token_fcm', ''));
  57. $token = $tokenFromRequest !== ''
  58. ? $tokenFromRequest
  59. : ($tokenFromSession !== '' ? $tokenFromSession : $tokenFromCookie);
  60. if ($token !== '') {
  61. User::assignUniqueFcmToken((int)auth()->id(), $token);
  62. $request->session()->put('token_fcm', $token);
  63. }
  64. }
  65. return $this->sendLoginResponse($request);
  66. }
  67. // If the login attempt was unsuccessful we will increment the number of attempts
  68. // to login and redirect the user back to the login form. Of course, when this
  69. // user surpasses their maximum number of attempts they will get locked out.
  70. $this->incrementLoginAttempts($request);
  71. return $this->sendFailedLoginResponse($request);
  72. }
  73. public function logout(Request $request)
  74. {
  75. if ($request->user()) {
  76. User::clearFcmToken((int)$request->user()->id);
  77. }
  78. return $this->traitLogout($request);
  79. }
  80. }