diff --git a/docker-compose.yaml b/docker-compose.yaml index 04166aa..d8fef67 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,4 +1,4 @@ -version: '2' +version: '2.1' services: apache-php: image: nimmis/apache-php7 diff --git a/src/init.php b/src/init.php index 8dcd708..6f046b5 100644 --- a/src/init.php +++ b/src/init.php @@ -10,8 +10,6 @@ ]); function loadCurentUserId($authSecret) { - return 1; // EXTENDED TASK: delete this line to complete the extended task - global $redis; // empty auth secret means the user is logged out @@ -20,10 +18,12 @@ function loadCurentUserId($authSecret) { } // use the auth secret to get the user ID - // $userId = _____________ (EXTENDED TASK) - if ($userId) { + $userId = $redis->hget("users", $authSecret); + + if (isset($userId)) { // cross check that this auth secret is also stored in the user hash - // $userAuthSecret = _____________ (EXTENDED TASK) + $userAuthSecret = $redis->hget("user:" . $userId, "authSecret"); + if ($userAuthSecret != $authSecret) { return null; } diff --git a/src/login.php b/src/login.php index e319f30..6340076 100644 --- a/src/login.php +++ b/src/login.php @@ -1,53 +1,71 @@ hget("users", $username); +} +catch(Exception $e){ + $userId = null; +} if ($userId) { - // user ID exists => continue with the login flow - // $realPassword = __________________ (EXTENDED TASK) - if ($password === $realPassword) { - doLogin($userId); - } else { - http_response_code(401); - echo 'This account already exists and entered password is incorrect!'; - exit; - } + // user ID exists => continue with the login flow + $realPassword = $redis->hget("user:" . $userId, "password"); + + if ($password === $realPassword) { + doLogin($userId); + } else { + http_response_code(401); + echo 'This account already exists and entered password is incorrect!'; + exit; + } } else { - // user ID does not exist => continue with the register flow - // obtain new user ID - // $userId = _________________ (EXTENDED TASK) - // store this user account into a hash - // ________________________ (EXTENDED TASK) - // store the user ID into a hash - this is needed to lookup user IDs by usernames - // ________________________ (EXTENDED TASK) - - // login the user - doLogin($userId); + + // user ID does not exist => continue with the register flow + // obtain new user ID + try{ + $last = $redis->llen("user_ids"); + } + catch(Exception $e){ + $last = 0; + } + $userId = $last + 1; + + // store this user account into a hash + $redis->lpush("user_ids", array($userId)); + + // store the user ID into a hash - this is needed to lookup user IDs by usernames + $redis->hmset("user:$userId", array( + "username" => "$username", + "password" => "$password", + )); + + $redis->hset("users", $username, $userId); + + // login the user + doLogin($userId); } function doLogin($userId) { - global $redis; + global $redis; - // calculate random user secret - $rand = rand(0, PHP_INT_MAX) . $userId; - $authSecret = hash('sha256', $rand); + // calculate random user secret + $rand = rand(0, PHP_INT_MAX) . $userId; + $authSecret = hash('sha256', $rand); - // delete the old auth secret (in case it exists) - // ________________________ (EXTENDED TASK) + // delete the old auth secret (in case it exists) + $redis->hdel("users", array($authSecret)); - // update the auth secret stored in the user hash - // ________________________ (EXTENDED TASK) + // update the auth secret stored in the user hash + $redis->hset("user:" . $userId, "authSecret", $authSecret); - // store the user ID into a hash - this is needed to lookup user IDs by user secrets - // ________________________ (EXTENDED TASK) + // store the user ID into a hash - this is needed to lookup user IDs by user secrets + $redis->hset("users", $authSecret, $userId); - setcookie("auth", $authSecret, time() + 3600 * 24 * 365); -} + setcookie("auth", $authSecret, time() + 3600 * 24 * 365); +} \ No newline at end of file diff --git a/src/logout.php b/src/logout.php index d1ff7f2..682ea6b 100644 --- a/src/logout.php +++ b/src/logout.php @@ -1,7 +1,5 @@ hget("user" . $userId, "authSecret"); +if($authSecret){ + $redis->hdel("users", array($authSecret)); +} + // set this user's auth secret to empty -// ___________________ (EXTENDED TASK) +$redis->hset("user:" . $userId, "authSecret", ""); + setcookie('auth', '', 1); diff --git a/src/sendMessage.php b/src/sendMessage.php index 9254db1..b292646 100644 --- a/src/sendMessage.php +++ b/src/sendMessage.php @@ -13,10 +13,15 @@ $text = $_POST['text']; // get the ID of the message -// $messageId = _______________ (BASIC TASK) +$last = $redis->llen("messages"); +$messageId = ++$last; // insert the message into its own hash -// _______________ (BASIC TASK) +$redis->hmset("message:" . $messageId, array( + "time" => $time, + "text" => $text, + "userId" => $userId +)); // push the message into the list of message IDs -// _______________ (BASIC TASK) +$redis->lpush("messages", array($messageId)); \ No newline at end of file diff --git a/src/showMessages.php b/src/showMessages.php index 338f694..5bf6074 100644 --- a/src/showMessages.php +++ b/src/showMessages.php @@ -10,15 +10,20 @@ } // get 10 latest messages -// $messages = _______________ (BASIC TASK) +$messages = $redis->lrange("messages", 0, 10); foreach ($messages as $id) { // get all properties of the message - // $message = _______________ (BASIC TASK) + $message = $redis->hgetall("message:" . $id); + + + if(isset($message["userId"])){ + // add the author's username to the message array + $message['username'] = $redis->hget("user:" . $message["userId"], "username"); + } else { + $message['username'] = "Annonymous"; + } - // add the author's username to the message array - $message['username'] = 'Anonymous'; - // $message['username'] = _____________ (EXTENDED TASK) printMessage($message); }