Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ <h1 id="header">Redis chat demo</h1>
});
});

text.addEventListener("keyup", function (event) {
event.preventDefault();
if (event.keyCode === 13) {
sendBtn.click();
}
});


setInterval(function(){
xhr('GET', 'showMessages.php', null, function(response){
Expand Down
11 changes: 6 additions & 5 deletions src/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

$redis = new Predis\Client([
'scheme' => 'tcp',
'host' => 'redis',
'host' => 'localhost',
'port' => 6379,
'async' => false,
'throw_errors' => true
]);

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
Expand All @@ -20,10 +20,11 @@ function loadCurentUserId($authSecret) {
}

// use the auth secret to get the user ID
// $userId = _____________ (EXTENDED TASK)
$userId = $redis->executeRaw(["GET", "authSecret_$authSecret"]);

if ($userId) {
// cross check that this auth secret is also stored in the user hash
// $userAuthSecret = _____________ (EXTENDED TASK)
$userAuthSecret = $redis->executeRaw(["HGET", $userId, "authSecret"]);
if ($userAuthSecret != $authSecret) {
return null;
}
Expand Down
20 changes: 10 additions & 10 deletions src/login.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
<?php

exit; // EXTENDED TASK: delete this line to complete the extended task

require "init.php";

$username = $_POST['username'];
$password = $_POST['password'];

// lookup the user IDs by username
// $userId = ___________________ (EXTENDED TASK)
$userId = $redis->executeRaw(["GET", "user_$username"]);

if ($userId) {
// user ID exists => continue with the login flow
// $realPassword = __________________ (EXTENDED TASK)
$realPassword = $redis->executeRaw(["HGET", $userId, "password"]);
if ($password === $realPassword) {
doLogin($userId);
} else {
Expand All @@ -23,11 +21,13 @@
} else {
// user ID does not exist => continue with the register flow
// obtain new user ID
// $userId = _________________ (EXTENDED TASK)
$userId = "user_" . $redis->executeRaw(["INCR", "user_sequence"]);

// store this user account into a hash
// ________________________ (EXTENDED TASK)
$redis->executeRaw(["HMSET", $userId, "username", $username, "password", "$password"]);

// store the user ID into a hash - this is needed to lookup user IDs by usernames
// ________________________ (EXTENDED TASK)
$redis->executeRaw(["SET", "user_$username", $userId]);

// login the user
doLogin($userId);
Expand All @@ -41,13 +41,13 @@ function doLogin($userId) {
$authSecret = hash('sha256', $rand);

// delete the old auth secret (in case it exists)
// ________________________ (EXTENDED TASK)
$redis->executeRaw(["HDEL", $userId, "authSecret"]);

// update the auth secret stored in the user hash
// ________________________ (EXTENDED TASK)
$redis->executeRaw(["HSET", $userId, "authSecret", $authSecret]);

// store the user ID into a hash - this is needed to lookup user IDs by user secrets
// ________________________ (EXTENDED TASK)
$redis->executeRaw(["SET", "authSecret_$authSecret", $userId]);

setcookie("auth", $authSecret, time() + 3600 * 24 * 365);
}
7 changes: 3 additions & 4 deletions src/logout.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

exit; // EXTENDED TASK: delete this line to complete the extended task

require "init.php";

$userId = loadCurentUserId($_COOKIE['auth']);
Expand All @@ -12,8 +10,9 @@
}

// delete the old auth secret
// ___________________ (EXTENDED TASK)
$redis->executeRaw(["HDEL", $userId, "authSecret"]);

// set this user's auth secret to empty
// ___________________ (EXTENDED TASK)
$redis->executeRaw(["DEL", "authSecret_$authSecret"]);

setcookie('auth', '', 1);
7 changes: 4 additions & 3 deletions src/sendMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
$text = $_POST['text'];

// get the ID of the message
// $messageId = _______________ (BASIC TASK)
$messageId = "message_" . $redis->executeRaw(["INCR", "message_sequence"]);

// insert the message into its own hash
// _______________ (BASIC TASK)
$username = $redis->executeRaw(["HGET", $userId, "username"]);
$redis->executeRaw(["HMSET", $messageId, "time", $time, "text", "$text", "username", $username]);

// push the message into the list of message IDs
// _______________ (BASIC TASK)
$redis->executeRaw(["RPUSH", "messages", $messageId]);
31 changes: 17 additions & 14 deletions src/showMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,30 @@
}

// get 10 latest messages
// $messages = _______________ (BASIC TASK)
$messages = $redis->executeRaw(["LRANGE", "messages", "0", "10"]);
$messages = array_reverse($messages);

foreach ($messages as $id) {
foreach ($messages as $messageId) {
// get all properties of the message
// $message = _______________ (BASIC TASK)
$message = [];
$message["text"] = $redis->executeRaw(["HGET", $messageId, "text"]);
$message["time"] = $redis->executeRaw(["HGET", $messageId, "time"]);

// add the author's username to the message array
$message['username'] = 'Anonymous';
// $message['username'] = _____________ (EXTENDED TASK)
$message['username'] = $redis->executeRaw(["HGET", $messageId, "username"]);

printMessage($message);
}

function printMessage(array $message) {
function printMessage(array $message)
{
echo "<table style=\"width:100%\">"
."<tr class='infoRow'>"
."<td class='userColumn'>" . $message['username'] . "</td>"
."<td class='timeColumn'>". date('m/d/Y H:i:s', $message['time']) ."</td>"
."</tr>"
."<tr class='messageRow'>"
."<td class='messageColumn'>". $message['text'] ."</td>"
."</tr>"
."</table>";
. "<tr class='infoRow'>"
. "<td class='userColumn'>" . $message['username'] . "</td>"
. "<td class='timeColumn'>" . date('m/d/Y H:i:s', $message['time']) . "</td>"
. "</tr>"
. "<tr class='messageRow'>"
. "<td class='messageColumn'>" . $message['text'] . "</td>"
. "</tr>"
. "</table>";
}