VerificationController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Recipient;
  5. use App\Models\User;
  6. use Illuminate\Auth\Access\AuthorizationException;
  7. use Illuminate\Auth\Events\Verified;
  8. use Illuminate\Foundation\Auth\VerifiesEmails;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Hash;
  11. class VerificationController extends Controller
  12. {
  13. /*
  14. |--------------------------------------------------------------------------
  15. | Email Verification Controller
  16. |--------------------------------------------------------------------------
  17. |
  18. | This controller is responsible for handling email verification for any
  19. | user that recently registered with the application. Emails may also
  20. | be re-sent if the user didn't receive the original email message.
  21. |
  22. */
  23. use VerifiesEmails;
  24. /**
  25. * Where to redirect users after verification.
  26. *
  27. * @var string
  28. */
  29. protected $redirectTo = '/';
  30. /**
  31. * Create a new controller instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct()
  36. {
  37. $this->middleware('auth')->except('verify');
  38. $this->middleware('signed')->only('verify');
  39. $this->middleware('throttle:1,1')->only('resend');
  40. $this->middleware('throttle:6,1')->only('verify');
  41. }
  42. /**
  43. * Mark the authenticated user's email address as verified.
  44. *
  45. * @param \Illuminate\Http\Request $request
  46. * @return \Illuminate\Http\Response
  47. *
  48. * @throws \Illuminate\Auth\Access\AuthorizationException
  49. */
  50. public function verify(Request $request)
  51. {
  52. $verifiable = User::find($request->route('id')) ?? Recipient::find($request->route('id'));
  53. if (is_null($verifiable)) {
  54. throw new AuthorizationException('Email address not found.');
  55. }
  56. if (! hash_equals((string) $request->route('id'), (string) $verifiable->getKey())) {
  57. throw new AuthorizationException('Invalid hash.');
  58. }
  59. if (! Hash::check($verifiable->getEmailForVerification(), (string) base64_decode($request->route('hash')))) {
  60. throw new AuthorizationException('Invalid hash.');
  61. }
  62. if ($verifiable->hasVerifiedEmail()) {
  63. return redirect($this->redirectPath());
  64. }
  65. if ($verifiable->markEmailAsVerified() && $verifiable instanceof User) {
  66. event(new Verified($verifiable));
  67. }
  68. if ($request->user() !== null) {
  69. $redirect = $verifiable instanceof User ? $this->redirectPath() : route('recipients.index');
  70. } else {
  71. $redirect = 'login';
  72. }
  73. return redirect($redirect)
  74. ->with('verified', true)
  75. ->with(['status' => 'Email Address Verified Successfully']);
  76. }
  77. }