VerificationController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. * @throws \Illuminate\Auth\Access\AuthorizationException
  48. */
  49. public function verify(Request $request)
  50. {
  51. $verifiable = User::find($request->route('id')) ?? Recipient::find($request->route('id'));
  52. if (is_null($verifiable)) {
  53. throw new AuthorizationException('Email address not found.');
  54. }
  55. if (! hash_equals((string) $request->route('id'), (string) $verifiable->getKey())) {
  56. throw new AuthorizationException('Invalid hash.');
  57. }
  58. if (! Hash::check($verifiable->getEmailForVerification(), (string) base64_decode($request->route('hash')))) {
  59. throw new AuthorizationException('Invalid hash.');
  60. }
  61. if ($verifiable->hasVerifiedEmail()) {
  62. return redirect($this->redirectPath());
  63. }
  64. if ($verifiable->markEmailAsVerified() && $verifiable instanceof User) {
  65. event(new Verified($verifiable));
  66. }
  67. if ($request->user() !== null) {
  68. $redirect = $verifiable instanceof User ? $this->redirectPath() : route('recipients.index');
  69. } else {
  70. $redirect = 'login';
  71. }
  72. return redirect($redirect)
  73. ->with('verified', true)
  74. ->with(['status' => 'Email Address Verified Successfully']);
  75. }
  76. }