<?php
namespace App\Controller;
use App\Service\AccessToken\AccessTokenService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends BaseAbstractController
{
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('index');
}
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
if ($error) {
switch ($error->getMessage()) {
case "The presented password is invalid.":
$error = 'Неверный логин или пароль';
break;
default:
$error = $error->getMessage();
}
// $this->addFlash('errors', $error->getMessage());
$this->addFlash('errors', $error);
}
return $this->render('security/login.html.twig', ['last_username' => $lastUsername]);
}
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
public function messengerLogout(AccessTokenService $accessTokenService, Session $session): Response
{
$token = $accessTokenService->getByCookies();
if ($token) {
$token->setRevoked(true);
if ($token->getRefreshToken()) {
$token->getRefreshToken()->setRevoked(true);
}
$this->em->flush();
}
if ($this->getLoggedInUser()) {
$session->invalidate();
$this->container->get('security.token_storage')->setToken(null);
}
$response = $this->redirectToRoute('messenger_login');
$response->headers->clearCookie('access_token', '/1bc3f6fdaea8f6ae35');
return $response;
// throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
public function messengerLogin(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('messenger_index');
}
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
if ($error) {
switch ($error->getMessage()) {
case "Bad credentials.":
$error = 'Неверный логин или пароль.';
break;
default:
$error = $error->getMessage();
}
$this->addFlash('errors', $error);
}
return $this->render('messenger/login.html.twig', [
'last_username' => $lastUsername
]);
}
}