src/EventSubscriber/CheckVerifiedUserSubscriber.php line 9

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\Event\CheckPassportEvent;
  7. class CheckVerifiedUserSubscriber implements EventSubscriberInterface
  8. {
  9.     public function onCheckPassport(CheckPassportEvent $event)
  10.     {
  11.         $passport $event->getPassport();
  12.         $user $passport->getUser();
  13.         if (!$user instanceof User) {
  14.             throw new CustomUserMessageAuthenticationException(
  15.                 'Something went wrong. Please try again.'
  16.             );
  17.         } else {
  18.             if (!$user->isVerified()) {
  19.                 throw new CustomUserMessageAuthenticationException(
  20.                     'Please verify your account before logging in.'
  21.                 );
  22.             }
  23.         }
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             CheckPassportEvent::class => ['onCheckPassport', -10],
  29.         ];
  30.     }
  31. }