src/Controller/SecurityController.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\AccessToken\AccessTokenService;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Session\Session;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. class SecurityController extends BaseAbstractController
  9. {
  10.     public function login(AuthenticationUtils $authenticationUtils): Response
  11.     {
  12.         if ($this->getUser()) {
  13.             return $this->redirectToRoute('index');
  14.         }
  15.         $error $authenticationUtils->getLastAuthenticationError();
  16.         $lastUsername $authenticationUtils->getLastUsername();
  17.         if ($error) {
  18.             switch ($error->getMessage()) {
  19.                 case "The presented password is invalid.":
  20.                     $error 'Неверный логин или пароль';
  21.                     break;
  22.                 default:
  23.                     $error $error->getMessage();
  24.             }
  25.             //  $this->addFlash('errors', $error->getMessage());
  26.              $this->addFlash('errors'$error);
  27.         }
  28.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername]);
  29.     }
  30.     public function logout(): void
  31.     {
  32.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  33.     }
  34.     public function messengerLogout(AccessTokenService $accessTokenServiceSession $session): Response
  35.     {
  36.         $token $accessTokenService->getByCookies();
  37.         if ($token) {
  38.             $token->setRevoked(true);
  39.             if ($token->getRefreshToken()) {
  40.                 $token->getRefreshToken()->setRevoked(true);
  41.             }
  42.             $this->em->flush();
  43.         }
  44.         if ($this->getLoggedInUser()) {
  45.             $session->invalidate();
  46.             $this->container->get('security.token_storage')->setToken(null);
  47.         }
  48.         $response $this->redirectToRoute('messenger_login');
  49.         $response->headers->clearCookie('access_token''/1bc3f6fdaea8f6ae35');
  50.         return $response;
  51. //        throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  52.     }
  53.     public function messengerLogin(AuthenticationUtils $authenticationUtils,
  54.            AccessTokenService $accessTokenService): Response
  55.     {
  56.         if ($this->getUser()) {
  57.             return $this->redirectToRoute('messenger_index');
  58.         }
  59.         $token $accessTokenService->getByCookies();
  60.         if ($token && $token->isRefreshable()) {
  61.             return $this->redirectToRoute("messenger_index");
  62.         }
  63.         $error $authenticationUtils->getLastAuthenticationError();
  64.         $lastUsername $authenticationUtils->getLastUsername();
  65.         if ($error) {
  66.             switch ($error->getMessage()) {
  67.                 case "Bad credentials.":
  68.                     $error 'Неверный логин или пароль.';
  69.                     break;
  70.                 default:
  71.                     $error $error->getMessage();
  72.             }
  73.             $this->addFlash('errors'$error);
  74.         }
  75.         return $this->render('messenger/login.html.twig', [
  76.             'last_username' => $lastUsername
  77.         ]);
  78.     }
  79. }