ForgotPasswordController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\User;
  5. use App\Models\Username;
  6. use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Password;
  9. class ForgotPasswordController extends Controller
  10. {
  11. /*
  12. |--------------------------------------------------------------------------
  13. | Password Reset Controller
  14. |--------------------------------------------------------------------------
  15. |
  16. | This controller is responsible for handling password reset emails and
  17. | includes a trait which assists in sending these notifications from
  18. | your application to your users. Feel free to explore this trait.
  19. |
  20. */
  21. use SendsPasswordResetEmails;
  22. /**
  23. * Create a new controller instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. $this->middleware('guest');
  30. $this->middleware('throttle:3,1')->only('sendResetLinkEmail');
  31. }
  32. /**
  33. * Send a reset link to the given user.
  34. *
  35. * @param \Illuminate\Http\Request $request
  36. * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
  37. */
  38. public function sendResetLinkEmail(Request $request)
  39. {
  40. $this->validateUsername($request);
  41. // Find the user_id and use that for the credentials
  42. $userId = Username::firstWhere('username', $request->username)?->user_id;
  43. // We will send the password reset link to this user. Once we have attempted
  44. // to send the link, we will examine the response then see the message we
  45. // need to show to the user. Finally, we'll send out a proper response.
  46. $response = $this->broker()->sendResetLink(
  47. ['id' => $userId]
  48. );
  49. return $response == Password::RESET_LINK_SENT
  50. ? $this->sendResetLinkResponse($request, $response)
  51. : $this->sendResetLinkFailedResponse($request, $response);
  52. }
  53. /**
  54. * Validate the email for the given request.
  55. *
  56. * @param \Illuminate\Http\Request $request
  57. * @return void
  58. */
  59. protected function validateUsername(Request $request)
  60. {
  61. $request->validate(['username' => 'required|regex:/^[a-zA-Z0-9]*$/|max:20']);
  62. }
  63. /**
  64. * Get the response for a failed password reset link.
  65. *
  66. * @param \Illuminate\Http\Request $request
  67. * @param string $response
  68. * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
  69. */
  70. protected function sendResetLinkFailedResponse(Request $request, $response)
  71. {
  72. return back()
  73. ->withInput($request->only('username'))
  74. ->withErrors(['username' => trans($response)]);
  75. }
  76. }