src/Controller/Represenatives/LoginController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Represenatives;
  3. use App\Form\LoginFormType;
  4. use Pimcore\Controller\FrontendController;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. class LoginController extends FrontendController
  12. {
  13.     /**
  14.      * @Route("/representative/login", name="representative-login")
  15.      */
  16.     public function loginAction(
  17.         AuthenticationUtils $authenticationUtils,
  18.         Request $request,
  19.         UserInterface $user = null
  20.     ): Response|RedirectResponse
  21.     {
  22.         //redirect user to index page if logged in
  23.         if ($user && $this->isGranted('ROLE_REPRESENTATIVE')) {
  24.             return $this->redirectToRoute('representatives-clients');
  25.         }
  26.         // get the login error if there is one
  27.         $error = $authenticationUtils->getLastAuthenticationError();
  28.         // last username entered by the user
  29.         $lastUsername = $authenticationUtils->getLastUsername();
  30.         $formData = [
  31.             '_username' => $lastUsername
  32.         ];
  33.         $form = $this->createForm(LoginFormType::class, $formData, [
  34.             'action' => $this->generateUrl('representative-login'),
  35.         ]);
  36.         //store referer in session to get redirected after login
  37.         if (!$request->get('no-referer-redirect')) {
  38.             $request->getSession()->set('_security.main.target_path', $request->headers->get('referer'));
  39.         }
  40.         return $this->render('representatives/representative-login.html.twig', [
  41.             'form' => $form->createView(),
  42.             'error' => $error,
  43.             'hideBreadcrumbs' => true
  44.         ]);
  45.     }
  46. }