register works, login almoast, token system in build
This commit is contained in:
@@ -48,6 +48,14 @@ class Handler
|
||||
]);
|
||||
return $statement->rowCount() > 0;
|
||||
}
|
||||
public function getPasswordHash(string $username): string
|
||||
{
|
||||
$statement = $this->pdo->prepare("SELECT password FROM users WHERE username = :username");
|
||||
$statement->execute([
|
||||
$username
|
||||
]);
|
||||
return $statement->fetch()["password"];
|
||||
}
|
||||
public function changeUsername(string $username, string $newUsername): void
|
||||
{
|
||||
$statement = $this->pdo->prepare("UPDATE users SET username = :newUsername WHERE username = :username");
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace ComCen\Http;
|
||||
|
||||
use ComCen\Database\Handler;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Ratchet\Http\HttpServerInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
@@ -11,20 +12,33 @@ class LoginController implements HttpServerInterface
|
||||
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null): void
|
||||
{
|
||||
$params = [];
|
||||
$login = true;
|
||||
$responseHead = "";
|
||||
$json = "";
|
||||
parse_str($request->getUri()->getQuery(), $params);
|
||||
|
||||
$username = $params["username"];
|
||||
$password = $params["password"];
|
||||
|
||||
if (!$username || !$password) {
|
||||
$json = json_encode(["error" => "Bad Credentials"]);
|
||||
Utils::class->sendJson($conn, "404 Bad Request", $json);
|
||||
if (!$username || !$password)
|
||||
{
|
||||
$login = false;
|
||||
$responseHead = "400";
|
||||
$json = json_encode(["error" => "Not enough params"]);
|
||||
}
|
||||
else if (password_verify($password, Handler::class->getPasswordHash($username)))
|
||||
|
||||
if (!$login)
|
||||
{
|
||||
Utils::class->responeJson($conn, $responseHead, $json);
|
||||
$conn->close();
|
||||
return;
|
||||
}
|
||||
|
||||
$json = json_encode(["token" => "token"]);
|
||||
Utils::class->sendJson($conn, "200 OK", $json);
|
||||
Handler::class->addUser($username, $password);
|
||||
|
||||
$json = json_encode(["error" => "none"]);
|
||||
Utils::class->responeJson($conn, "200", $json);
|
||||
$conn->close();
|
||||
}
|
||||
|
||||
|
||||
@@ -12,20 +12,44 @@ class RegisterController implements HttpServerInterface
|
||||
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null): void
|
||||
{
|
||||
$params = [];
|
||||
$createAccount = true;
|
||||
$responseHead = "";
|
||||
$json = "";
|
||||
parse_str($request->getUri()->getQuery(), $params);
|
||||
|
||||
$username = $params["username"];
|
||||
$password = $params["password"];
|
||||
|
||||
if (!$username || !$password) {
|
||||
$json = json_encode(["error" => "Bad Credentials"]);
|
||||
Utils::class->sendJson($conn, "404 Bad Request", $json);
|
||||
if (!$username || !$password)
|
||||
{
|
||||
$createAccount = false;
|
||||
$responseHead = "400";
|
||||
$json = json_encode(["error" => "Not enough params"]);
|
||||
}
|
||||
else if (count($password) < 5)
|
||||
{
|
||||
$createAccount = false;
|
||||
$responseHead = "400";
|
||||
$json = json_encode(["error" => "Short password"]);
|
||||
}
|
||||
else if (Handler::class->userExists($username))
|
||||
{
|
||||
$createAccount = false;
|
||||
$responseHead = "409";
|
||||
$json = json_encode(["error" => "Taken"]);
|
||||
}
|
||||
|
||||
if (!$createAccount)
|
||||
{
|
||||
Utils::class->responeJson($conn, $responseHead, $json);
|
||||
$conn->close();
|
||||
return;
|
||||
}
|
||||
|
||||
$json = json_encode(["token" => "token"]);
|
||||
Utils::class->sendJson($conn, "200 OK", $json);
|
||||
Handler::class->addUser($username, $password);
|
||||
|
||||
$json = json_encode(["error" => "none"]);
|
||||
Utils::class->responeJson($conn, "200", $json);
|
||||
$conn->close();
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ use Ratchet\ConnectionInterface;
|
||||
|
||||
class Utils
|
||||
{
|
||||
function sendJson(ConnectionInterface $conn, string $head, string $jsonData): void
|
||||
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}");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace ComCen\Security;
|
||||
|
||||
class TokenHandler
|
||||
{
|
||||
private static ?self $instance = null;
|
||||
private static $tokens = [];
|
||||
public static function getNewTokenForUser(string $username)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user