1. Go to this page and download the library: Download danielburger1337/2fa-email library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
declare(strict_types=1);
use danielburger1337\SchebTwoFactorBundle\Mailer\AuthCodeMailerInterface;
use danielburger1337\SchebTwoFactorBundle\Model\TwoFactorEmailInterface;
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
#[AsEventListener(RequestEvent::class)]
class ResendEmailAuthCodeEventListener
{
public function __construct(
private readonly RateLimiterFactory $rateLimiterFactory,
private readonly AuthCodeMailerInterface $authCodeMailer,
private readonly TokenStorageInterface $tokenStorage,
) {
}
public function __invoke(RequestEvent $request): void
{
$request = $event->getRequest();
if ($request->attributes->get('_route') !== '2fa_login') {
return;
}
$token = $this->tokenStorage->getToken();
$user = $token?->getUser();
if (!$token instanceof TwoFactorTokenInterface || !$user instanceof TwoFactorEmailInterface) {
return;
}
// somehow determine that you want to resend the email
if ($request->request->get('resendAuthCode') === 'true') {
// If you use rate limiting, make sure to also use the auth code as key,
// otherwise the user might get throttled when their code has expired and a new one should be sent.
$rateLimiter = $this->rateLimiterFactory->create(
'tfa_email_'.\hash('xxh128', $user->getEmailAuthCode().$user->getEmailAuthRecipient())
);
if ($rateLimiter->consume(1)->isAccepted()) {
// mail the auth code
$this->authCodeMailer->sendAuthCode($user);
}
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.