<?php
namespace App\Controller;
use App\Classes\General;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class LoginController extends AbstractController
{
/**
* @Route("/login", name="login")
*/
public function login(Request $request)
{
$gen = new General($request);
if ($gen->checkIsLogin()) {
return $this->redirect($this->generateUrl("users_data"));
}
$data = [];
$form = $this->createFormBuilder()
->add("username", TextType::class)
->add('password', PasswordType::class)
->getForm();
$form->handleRequest($request);
if ($request->getMethod() == "POST") {
if ($form->isValid()) {
$password = md5($form['password']->getData());
$admin = $this->getDoctrine()->getRepository("App:Admin")->findOneBy([
"username" => $form['username']->getData(),
"password" => $password
]);
if ($admin) {
$session = $this->get("session");
$session->set("isLogin",true);
$session->set("currentUserObj",$admin);
return $this->redirect($this->generateUrl("users_data"));
} else {
$data['error'] = "400";
}
}
}
$data['form'] = $form->createView();
return $this->render('login/login.html.twig', $data);
}
/**
* @Route("/logout",name="logout")
*/
public function logout(Request $request)
{
$session = $request->getSession();
$session->set("isLogin", false);
$session->set("currentUserObj", "");
return $this->redirect($this->generateUrl("login"));
}
}