src/Security/AppCustomAuthenticator.php line 22
<?phpnamespace App\Security;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Bundle\SecurityBundle\Security;use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;use Symfony\Component\Security\Http\Authenticator\Passport\Passport;use Symfony\Component\Security\Http\Util\TargetPathTrait;use App\Classes\Utils;use App\Entity\UserLogin;use Doctrine\ORM\EntityManagerInterface;class AppCustomAuthenticator extends AbstractLoginFormAuthenticator{use TargetPathTrait;public const LOGIN_ROUTE = 'app_login';protected $entityManager = null;public function __construct(private UrlGeneratorInterface $urlGenerator, EntityManagerInterface $entityManager){$this->entityManager = $entityManager;}public function authenticate(Request $request): Passport{$username = $request->request->get('username', '');$request->getSession()->set(Security::LAST_USERNAME, $username);return new Passport(new UserBadge($username),new PasswordCredentials($request->request->get('password', '')),[new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),]);}public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response{$userLogin = new UserLogin();$userLogin->setIp(Utils::getUserIP());$userLogin->setInformacionAdicional($_SERVER['HTTP_USER_AGENT']);$userLogin->setFecha(new \DateTime());$userLogin->setUser($token->getUser());$userLogin->setTipo(UserLogin::FORM_LOGIN);$this->entityManager->persist($userLogin);$this->entityManager->flush();return new RedirectResponse($this->urlGenerator->generate('homepage'));}protected function getLoginUrl(Request $request): string{return $this->urlGenerator->generate(self::LOGIN_ROUTE);}}