now most of logic works as it should
This commit is contained in:
@@ -38,7 +38,7 @@ class WebSocketServer implements MessageComponentInterface
|
|||||||
{
|
{
|
||||||
foreach ($this->connectionsData as $connectionData) {
|
foreach ($this->connectionsData as $connectionData) {
|
||||||
if ($connectionData["username"] !== $username) {
|
if ($connectionData["username"] !== $username) {
|
||||||
$connectionData["connection"]->send("{\"sender\": \"{$username}\",\"msg\": \"{$msg}\"");
|
$connectionData["connection"]->send("{\"sender\": \"{$username}\",\"msg\": \"{$msg}\"}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ class WebSocketServer implements MessageComponentInterface
|
|||||||
if ($index === null) return;
|
if ($index === null) return;
|
||||||
|
|
||||||
if ($this->connectionsData[$index]["username"]) {
|
if ($this->connectionsData[$index]["username"]) {
|
||||||
$msgContent = $decodedMsg["msg"] ?? null;
|
$msgContent = $decodedMsg["message"] ?? null;
|
||||||
if ($msgContent) {
|
if ($msgContent) {
|
||||||
$this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $msgContent);
|
$this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $msgContent);
|
||||||
$from->send("{\"success\"}: \"message send\"");
|
$from->send("{\"success\"}: \"message send\"");
|
||||||
|
|||||||
+3
-3
@@ -82,13 +82,13 @@ $html->content = <<<'HTML'
|
|||||||
|
|
||||||
ws.onmessage = (event) => {
|
ws.onmessage = (event) => {
|
||||||
const text = event.data;
|
const text = event.data;
|
||||||
if (text === 'authenticated') {
|
if (text === '{"success"}: "authenticated"') {
|
||||||
log('Authenticated.', 'system');
|
log('Authenticated.', 'system');
|
||||||
setAuthenticated(true);
|
setAuthenticated(true);
|
||||||
} else if (text === 'invalid token') {
|
} else if (text === '{"error"}: "invalid token"') {
|
||||||
log('Invalid token. Connection closed.', 'error');
|
log('Invalid token. Connection closed.', 'error');
|
||||||
ws.close();
|
ws.close();
|
||||||
} else if (text === 'not authenticated') {
|
} else if (text === '{"error"}: "you are not authenticated"') {
|
||||||
log('Server: not authenticated', 'error');
|
log('Server: not authenticated', 'error');
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -60,11 +60,11 @@ class Handler
|
|||||||
}
|
}
|
||||||
public function userExists(string $username): bool
|
public function userExists(string $username): bool
|
||||||
{
|
{
|
||||||
$statement = $this->pdo->prepare("SELECT * FROM users WHERE username = :username");
|
$statement = $this->pdo->prepare("SELECT 1 FROM users WHERE username = :username");
|
||||||
$statement->execute([
|
$statement->execute([
|
||||||
$username
|
"username" => $username
|
||||||
]);
|
]);
|
||||||
return $statement->rowCount() > 0;
|
return (bool) $statement->fetchColumn();
|
||||||
}
|
}
|
||||||
public function getPasswordHash(string $username): string
|
public function getPasswordHash(string $username): string
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ class LoginController implements HttpServerInterface
|
|||||||
$responseHead = "400";
|
$responseHead = "400";
|
||||||
$json = json_encode(["error" => "Not enough params"]);
|
$json = json_encode(["error" => "Not enough params"]);
|
||||||
}
|
}
|
||||||
else if (!Handler::getInstance()->userExists($username) == !password_verify($password, Handler::getInstance()->getPasswordHash($username)))
|
else if (!Handler::getInstance()->userExists($username) || !password_verify($password, Handler::getInstance()->getPasswordHash($username)))
|
||||||
{
|
{
|
||||||
$login = false;
|
$login = false;
|
||||||
$responseHead = "409";
|
$responseHead = "409";
|
||||||
|
|||||||
Reference in New Issue
Block a user