From cfc4e224970c719bd9b4f933133ddc7c961499d8 Mon Sep 17 00:00:00 2001 From: tracer Date: Mon, 24 Oct 2022 20:19:28 +0200 Subject: [PATCH] initial commit --- src/Controller/SecurityController.php | 64 +++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/Controller/SecurityController.php diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php new file mode 100644 index 0000000..2d60f22 --- /dev/null +++ b/src/Controller/SecurityController.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace App\Controller; + +use App\Entity\User; +use App\Repository\UserRepository; +use App\Service\Router; +use App\Service\Template; + +class SecurityController +{ + public function __construct( + private readonly Template $template, + private readonly UserRepository $userRepository, + private readonly Router $router + ) + { + + } + + public function login(): never + { + if (!empty($_POST)) { + $nick = $_POST['nick'] ?? ''; + $password = $_POST['password'] ?? ''; + + if ($nick && $password) { + if ($user = $this->userRepository->findbyNick(nick: $nick)) { + if (password_verify(password: $password, hash: $user->getPassword())) { + $_SESSION['user_id'] = $user->getId(); + header(header: 'Location: /'); + exit(0); + } else { + $message = "Wrong credentials."; + } + } else { + $message = "User not found."; + } + } else { + $message = 'You need to enter your credentials.'; + } + } + + $this->template->render(templateName: 'security/login.html.php', vars: [ + 'user' => $user ?? new User(), + 'message' => $message ?? '', + 'router' => $this->router + ]); + } + + function logout(): void + { + session_unset(); + header(header: 'Location: /'); + } + +} \ No newline at end of file