<?php
namespace App\Controller\Represenatives;
use App\Form\LoginFormType;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends FrontendController
{
/**
* @Route("/representative/login", name="representative-login")
*/
public function loginAction(
AuthenticationUtils $authenticationUtils,
Request $request,
UserInterface $user = null
): Response|RedirectResponse
{
//redirect user to index page if logged in
if ($user && $this->isGranted('ROLE_REPRESENTATIVE')) {
return $this->redirectToRoute('representatives-clients');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$formData = [
'_username' => $lastUsername
];
$form = $this->createForm(LoginFormType::class, $formData, [
'action' => $this->generateUrl('representative-login'),
]);
//store referer in session to get redirected after login
if (!$request->get('no-referer-redirect')) {
$request->getSession()->set('_security.main.target_path', $request->headers->get('referer'));
}
return $this->render('representatives/representative-login.html.twig', [
'form' => $form->createView(),
'error' => $error,
'hideBreadcrumbs' => true
]);
}
}