src/MyCmcmApi/EventListener/AuthenticationEventListener.php line 11

Open in your IDE?
  1. <?php
  2. namespace PaperKite\MyCmcmApi\EventListener;
  3. use Exception;
  4. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  5. use Lightbulb\Symfony\Exception\ForbiddenException;
  6. use PaperKite\MyCmcmApi\Entity\MyCmcmApiUserInterface;
  7. use PaperKite\MyCmcmApi\Service\TwoFactorAuthenticationService;
  8. class AuthenticationEventListener
  9. {
  10.     public function __construct(
  11.         private TwoFactorAuthenticationService $twoFactorAuthenticationService,
  12.     ) {
  13.     }
  14.     /**
  15.      * @throws ForbiddenException
  16.      * @throws Exception
  17.      */
  18.     public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event): void
  19.     {
  20.         $user $event->getUser();
  21.         if (true === $user instanceof MyCmcmApiUserInterface) {
  22.             if (null === $user->getValidatedAt()) {
  23.                 throw new ForbiddenException('Email must be validated to login');
  24.             }
  25.             // enable 2FA
  26.             $twoFactorAuthenticationId $this->twoFactorAuthenticationService->runTwoFactorAuthentication($user->getMyCmcmUser2fa());
  27.             $event->setData([
  28.                 'twoFactorAuthenticationId' => $twoFactorAuthenticationId,
  29.             ]);
  30.         }
  31.     }
  32. }