<?php
namespace PaperKite\MyCmcmApi\EventListener;
use Exception;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Lightbulb\Symfony\Exception\ForbiddenException;
use PaperKite\MyCmcmApi\Entity\MyCmcmApiUserInterface;
use PaperKite\MyCmcmApi\Service\TwoFactorAuthenticationService;
class AuthenticationEventListener
{
public function __construct(
private TwoFactorAuthenticationService $twoFactorAuthenticationService,
) {
}
/**
* @throws ForbiddenException
* @throws Exception
*/
public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event): void
{
$user = $event->getUser();
if (true === $user instanceof MyCmcmApiUserInterface) {
if (null === $user->getValidatedAt()) {
throw new ForbiddenException('Email must be validated to login');
}
// enable 2FA
$twoFactorAuthenticationId = $this->twoFactorAuthenticationService->runTwoFactorAuthentication($user->getMyCmcmUser2fa());
$event->setData([
'twoFactorAuthenticationId' => $twoFactorAuthenticationId,
]);
}
}
}