src/Controller/LoginController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Classes\General;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  6. use Symfony\Component\Form\Extension\Core\Type\TextType;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class LoginController extends AbstractController
  10. {
  11.     /**
  12.      * @Route("/login", name="login")
  13.      */
  14.     public function login(Request $request)
  15.     {
  16.         $gen = new General($request);
  17.         if ($gen->checkIsLogin()) {
  18.             return $this->redirect($this->generateUrl("users_data"));
  19.         }
  20.         $data = [];
  21.         $form $this->createFormBuilder()
  22.             ->add("username"TextType::class)
  23.             ->add('password'PasswordType::class)
  24.             ->getForm();
  25.         $form->handleRequest($request);
  26.         if ($request->getMethod() == "POST") {
  27.             if ($form->isValid()) {
  28.                 $password md5($form['password']->getData());
  29.                 $admin $this->getDoctrine()->getRepository("App:Admin")->findOneBy([
  30.                     "username"  =>  $form['username']->getData(),
  31.                     "password"  =>  $password
  32.                 ]);
  33.                 if ($admin) {
  34.                     $session $this->get("session");
  35.                     $session->set("isLogin",true);
  36.                     $session->set("currentUserObj",$admin);
  37.                     return $this->redirect($this->generateUrl("users_data"));
  38.                 } else {
  39.                     $data['error'] = "400";
  40.                 }
  41.             }
  42.         }
  43.         $data['form'] = $form->createView();
  44.         return $this->render('login/login.html.twig'$data);
  45.     }
  46.     /**
  47.      * @Route("/logout",name="logout")
  48.      */
  49.     public function logout(Request $request)
  50.     {
  51.         $session $request->getSession();
  52.         $session->set("isLogin"false);
  53.         $session->set("currentUserObj""");
  54.         return $this->redirect($this->generateUrl("login"));
  55.     }
  56. }