src/Controller/Admin/LoginController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Security\AdminUser;
  4. use App\Service\AdminService;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  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\Exception\UserNotFoundException;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. class LoginController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/login", name="app_login")
  15.      */
  16.     public function index(AuthenticationUtils $authenticationUtilsRequest $requestAdminService $admin): Response
  17.     {
  18.       // if already auth redirect do homepage
  19.       $test=$admin->getAdminUserFromSession($request);
  20.       if($test instanceof AdminUser)
  21.       {
  22.         return $this->redirectToRoute("admin_index");
  23.       }
  24.       // get the login error if there is one
  25.       //$error = $authenticationUtils->getLastAuthenticationError();
  26.       $error='';
  27.       $lastUsername='';
  28.       // last username entered by the user
  29.       //$lastUsername = $authenticationUtils->getLastUsername();
  30.       if($request->isMethod('POST'))
  31.       {
  32.         $username $request->request->get('_username');
  33.         $lastUsername=$username;
  34.         $password $request->request->get('_password');
  35.         try 
  36.         {
  37.           $user=$admin->loginUserAndCreateSessionData($username$password);
  38.         }
  39.         catch(UserNotFoundException $th)
  40.         {
  41.           $error="Login failed, check your username and password";
  42.         }
  43.         catch(\Throwable $th)
  44.         {
  45.           //throw new UserNotFoundException();
  46.           //$error="Login faiol;ed, check your username and password".$th->getMessage();
  47.           //$error="Login failed, check your username and password";
  48.           $error="Login failed: ".$th->getMessage();
  49.         }
  50.         if(!strlen($error))
  51.         {
  52.           return $this->redirectToRoute("admin_index");
  53.         }
  54.       }
  55.       return $this->render('admin/login/index.html.twig', [
  56.         'last_username' => $lastUsername,
  57.         'error'         => $error,        
  58.         //'controller_name' => 'LoginController',
  59.       ]);
  60.     }
  61.     /**
  62.      * @Route("/logout", name="app_logout")
  63.      */
  64.     public function logout(AuthenticationUtils $authenticationUtilsRequest $requestAdminService $admin): Response
  65.     {
  66.       $admin->logoutAdminUser($request);
  67.       return $this->redirectToRoute("app_login");
  68.     }
  69. }