VerificationController.php 2.6 KB

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