<?php
namespace App\EventSubscriber;
use App\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Event\CheckPassportEvent;
class CheckVerifiedUserSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CheckPassportEvent::class => [
'onCheckPassport', -10,
],
];
}
public function onCheckPassport(CheckPassportEvent $event)
{
$passport = $event->getPassport();
if ($passport instanceof Passport) {
$user = $passport->getUser();
if ($user instanceof User) {
if (!$user->isVerified()) {
throw new CustomUserMessageAuthenticationException('Please verify your account before logging in.');
}
}
}
}
}