src/EventSubscriber/CheckVerifiedUserSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  6. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  7. use Symfony\Component\Security\Http\Event\CheckPassportEvent;
  8. class CheckVerifiedUserSubscriber implements EventSubscriberInterface
  9. {
  10.     public static function getSubscribedEvents(): array
  11.     {
  12.         return [
  13.             CheckPassportEvent::class => [
  14.                 'onCheckPassport', -10,
  15.             ],
  16.         ];
  17.     }
  18.     public function onCheckPassport(CheckPassportEvent $event)
  19.     {
  20.         $passport $event->getPassport();
  21.         if ($passport instanceof Passport) {
  22.             $user $passport->getUser();
  23.             if ($user instanceof User) {
  24.                 if (!$user->isVerified()) {
  25.                     throw new CustomUserMessageAuthenticationException('Please verify your account before logging in.');
  26.                 }
  27.             }
  28.         }
  29.     }
  30. }