From d51eedd7bd0217a9293298c67136dacd07e9f1c9 Mon Sep 17 00:00:00 2001 From: GitProtogen Date: Fri, 6 Mar 2026 10:28:01 +0100 Subject: [PATCH] login register token logic is ready. Fixed also some syntax bugs --- src/Database/Handler.php | 8 ++++++++ src/Http/LoginController.php | 18 ++++++++++-------- src/Http/RegisterController.php | 10 +++++----- src/Http/Utils.php | 2 +- src/Security/TokenHandler.php | 8 ++++++++ 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/Database/Handler.php b/src/Database/Handler.php index fd46b35..3a8c807 100644 --- a/src/Database/Handler.php +++ b/src/Database/Handler.php @@ -9,6 +9,14 @@ class Handler private static ?Handler $instance = null; private PDO $pdo; + public static function getInstance(): static + { + if (self::$instance === null) { + self::$instance = new static(); + } + return self::$instance; + } + private function __construct() { $this->pdo = new PDO('sqlite:' . __DIR__ . '/../../storage/database.sqlite'); diff --git a/src/Http/LoginController.php b/src/Http/LoginController.php index 3c9a2a2..479aaaf 100644 --- a/src/Http/LoginController.php +++ b/src/Http/LoginController.php @@ -3,6 +3,7 @@ namespace ComCen\Http; use ComCen\Database\Handler; +use ComCen\Security\TokenHandler; use Ratchet\ConnectionInterface; use Ratchet\Http\HttpServerInterface; use Psr\Http\Message\RequestInterface; @@ -26,24 +27,25 @@ class LoginController implements HttpServerInterface $responseHead = "400"; $json = json_encode(["error" => "Not enough params"]); } - else if (!Handler::class->userExists($username) == !password_verify($password, Handler::class->getPasswordHash($username))) + else if (!Handler::getInstance()->userExists($username) == !password_verify($password, Handler::getInstance()->getPasswordHash($username))) { $login = false; - $responseHead = "400"; + $responseHead = "409"; $json = json_encode(["error" => "Bad"]); } - if (!$login) - { - Utils::class->responeJson($conn, $responseHead, $json); + if (!$login) { + Utils::responeJson($conn, $responseHead, $json); $conn->close(); return; } - Handler::class->addUser($username, $password); + if (TokenHandler::doesUserHaveToken($username)) { + TokenHandler::deleteTokensForUser($username); + } - $json = json_encode(["error" => "none"]); - Utils::class->responeJson($conn, "200", $json); + $json = json_encode(["token" => TokenHandler::getNewTokenForUser($username)]); + Utils::responeJson($conn, "200", $json); $conn->close(); } diff --git a/src/Http/RegisterController.php b/src/Http/RegisterController.php index 9a2d967..09c1a81 100644 --- a/src/Http/RegisterController.php +++ b/src/Http/RegisterController.php @@ -26,13 +26,13 @@ class RegisterController implements HttpServerInterface $responseHead = "400"; $json = json_encode(["error" => "Not enough params"]); } - else if (count($password) < 5) + else if (strlen($password) < 5) { $createAccount = false; $responseHead = "400"; $json = json_encode(["error" => "Short password"]); } - else if (Handler::class->userExists($username)) + else if (Handler::getInstance()->userExists($username)) { $createAccount = false; $responseHead = "409"; @@ -41,15 +41,15 @@ class RegisterController implements HttpServerInterface if (!$createAccount) { - Utils::class->responeJson($conn, $responseHead, $json); + Utils::responeJson($conn, $responseHead, $json); $conn->close(); return; } - Handler::class->addUser($username, $password); + Handler::getInstance()->addUser($username, $password); $json = json_encode(["error" => "none"]); - Utils::class->responeJson($conn, "200", $json); + Utils::responeJson($conn, "200", $json); $conn->close(); } diff --git a/src/Http/Utils.php b/src/Http/Utils.php index 6d8d84a..1f7c98c 100644 --- a/src/Http/Utils.php +++ b/src/Http/Utils.php @@ -6,7 +6,7 @@ use Ratchet\ConnectionInterface; class Utils { - function responeJson(ConnectionInterface $conn, string $head, string $jsonData): void + static function responeJson(ConnectionInterface $conn, string $head, string $jsonData): void { $conn->send("HTTP/1.1 {$head}\r\nContent-Type: application/json\r\n\r\n{$jsonData}"); } diff --git a/src/Security/TokenHandler.php b/src/Security/TokenHandler.php index 8e21366..a0d0bc7 100644 --- a/src/Security/TokenHandler.php +++ b/src/Security/TokenHandler.php @@ -47,4 +47,12 @@ class TokenHandler } } } + public static function deleteTokensForUser(string $user) + { + for ($i = 0; $i < count(self::$tokens); ++$i) { + if (self::$tokens[$i][0] === $user) { + array_splice(self::$tokens, $i, 1); + } + } + } } \ No newline at end of file