src/Security/LoginAuthenticator.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Smakmedia\Security;
  4. use Smakmedia\Repository\AdminUserRepository;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  16. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  17. class LoginAuthenticator extends AbstractLoginFormAuthenticator
  18. {
  19.     use TargetPathTrait;
  20.     private UrlGeneratorInterface $urlGenerator;
  21.     private AdminUserRepository $adminUserRepository;
  22.     public function __construct(UrlGeneratorInterface $urlGeneratorAdminUserRepository $adminUserRepository)
  23.     {
  24.         $this->urlGenerator $urlGenerator;
  25.         $this->adminUserRepository $adminUserRepository;
  26.     }
  27.     public function authenticate(Request $request): Passport
  28.     {
  29.         $credentials $this->getCredentials($request);
  30.         return new Passport(
  31.             new UserBadge($credentials['username'], function (string $userIdentifier) {
  32.                 return $this->adminUserRepository->findOneBy(['username' => $userIdentifier'status' => 1]);
  33.             }),
  34.             new PasswordCredentials($credentials['password']),
  35.             [new CsrfTokenBadge('authenticate'$credentials['csrf_token'])]
  36.         );
  37.     }
  38.     public function getCredentials(Request $request): array
  39.     {
  40.         $credentials = [
  41.             'username' => $request->request->get('_username'),
  42.             'password' => $request->request->get('_password'),
  43.             'csrf_token' => $request->request->get('_csrf_token'),
  44.         ];
  45.         $request->getSession()->set(
  46.             Security::LAST_USERNAME,
  47.             $credentials['username']
  48.         );
  49.         return $credentials;
  50.     }
  51.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  52.     {
  53.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  54.             return new RedirectResponse($targetPath);
  55.         }
  56.         return new RedirectResponse($this->urlGenerator->generate('dashboard'));
  57.     }
  58.     protected function getLoginUrl(Request $request): string
  59.     {
  60.         return $this->urlGenerator->generate('login');
  61.     }
  62. }