simplified sending responese
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace ComCen\Database;
|
||||
|
||||
use PDO;
|
||||
|
||||
class Handler
|
||||
{
|
||||
private static ?Handler $instance = null;
|
||||
private PDO $pdo;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
$this->pdo = new PDO('sqlite:' . __DIR__ . '/../../storage/database.sqlite');
|
||||
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
|
||||
public function init(): void
|
||||
{
|
||||
$this->pdo->exec("
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
username VARCHAR(32) NOT NULL UNIQUE,
|
||||
password CHAR(255) NOT NULL
|
||||
)
|
||||
");
|
||||
}
|
||||
|
||||
public function addUser(string $username, string $password): void
|
||||
{
|
||||
$statement = $this->pdo->prepare("INSERT INTO users (username, password) VALUES (:username, :password)");
|
||||
$statement->execute([
|
||||
"username" => $username,
|
||||
"password" => password_hash($password, PASSWORD_ARGON2ID)
|
||||
]);
|
||||
}
|
||||
public function deleteUser(string $username): void
|
||||
{
|
||||
$statement = $this->pdo->prepare("DELETE FROM users WHERE username = :username");
|
||||
$statement->execute([
|
||||
"username" => $username
|
||||
]);
|
||||
}
|
||||
public function userExists(string $username): bool
|
||||
{
|
||||
$statement = $this->pdo->prepare("SELECT * FROM users WHERE username = :username");
|
||||
$statement->execute([
|
||||
$username
|
||||
]);
|
||||
return $statement->rowCount() > 0;
|
||||
}
|
||||
public function changeUsername(string $username, string $newUsername): void
|
||||
{
|
||||
$statement = $this->pdo->prepare("UPDATE users SET username = :newUsername WHERE username = :username");
|
||||
$statement->execute([
|
||||
"username" => $username,
|
||||
"newUsername" => $newUsername
|
||||
]);
|
||||
}
|
||||
public function changePassword(string $username, string $newPassword): void
|
||||
{
|
||||
$statement = $this->pdo->prepare("UPDATE users SET password = :newPassword WHERE username = :username");
|
||||
$statement->execute([
|
||||
"username" => $username,
|
||||
"newPassword" => $newPassword
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,6 @@ class LoginController implements HttpServerInterface
|
||||
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null): void
|
||||
{
|
||||
$params = [];
|
||||
$json = "";
|
||||
parse_str($request->getUri()->getQuery(), $params);
|
||||
|
||||
$username = $params["username"];
|
||||
@@ -19,13 +18,13 @@ class LoginController implements HttpServerInterface
|
||||
|
||||
if (!$username || !$password) {
|
||||
$json = json_encode(["error" => "Bad Credentials"]);
|
||||
$conn->send("HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\n\r\n{$json}");
|
||||
Utils::class->sendJson($conn, "404 Bad Request", $json);
|
||||
$conn->close();
|
||||
return;
|
||||
}
|
||||
|
||||
$json = json_encode(["token" => "token"]);
|
||||
$conn->send("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{$json}");
|
||||
Utils::class->sendJson($conn, "200 OK", $json);
|
||||
$conn->close();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,13 +5,13 @@ namespace ComCen\Http;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Ratchet\Http\HttpServerInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use ComCen\Database\Handler;
|
||||
|
||||
class RegisterController implements HttpServerInterface
|
||||
{
|
||||
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null): void
|
||||
{
|
||||
$params = [];
|
||||
$json = "";
|
||||
parse_str($request->getUri()->getQuery(), $params);
|
||||
|
||||
$username = $params["username"];
|
||||
@@ -19,13 +19,13 @@ class RegisterController implements HttpServerInterface
|
||||
|
||||
if (!$username || !$password) {
|
||||
$json = json_encode(["error" => "Bad Credentials"]);
|
||||
$conn->send("HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\n\r\n{$json}");
|
||||
Utils::class->sendJson($conn, "404 Bad Request", $json);
|
||||
$conn->close();
|
||||
return;
|
||||
}
|
||||
|
||||
$json = json_encode(["token" => "token"]);
|
||||
$conn->send("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{$json}");
|
||||
Utils::class->sendJson($conn, "200 OK", $json);
|
||||
$conn->close();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace ComCen\Http;
|
||||
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
class Utils
|
||||
{
|
||||
function sendJson(ConnectionInterface $conn, string $head, string $jsonData): void
|
||||
{
|
||||
$conn->send("HTTP/1.1 {$head}\r\nContent-Type: application/json\r\n\r\n{$jsonData}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user