diff --git a/client/public/index.html b/client/public/index.html
index bc8c06c..250ec29 100644
--- a/client/public/index.html
+++ b/client/public/index.html
@@ -3,6 +3,7 @@
+
Bruh online
@@ -13,7 +14,7 @@
- awaiting client id
+ Awaiting ClientID
diff --git a/client/public/index.js b/client/public/index.js
index 0c59145..32a70d1 100644
--- a/client/public/index.js
+++ b/client/public/index.js
@@ -1,5 +1,9 @@
-const UsernameForm = document.querySelector('#input-username');
+const UsernameForm = document.querySelector('#input-username');
const UsernameInput = document.querySelector('#input-text-username');
+
+const ClientID = document.querySelector('#client-id');
+const ConnectionState = document.querySelector('#connection-state');
+
UsernameForm.addEventListener('submit', onUsernameSubmit);
(()=>{
@@ -31,4 +35,22 @@ async function onUsernameSubmit(e)
console.log(body);
+ if (body.errors)
+ {
+ ConnectionState.classList.add('red');
+ ConnectionState.innerHTML = `ERROR: ${body.errors[body.errors.length - 1].desc}`;
+ return;
+ }
+
+ if (ConnectionState.classList.contains('red'))
+ ConnectionState.classList.remove('red');
+ ConnectionState.innerHTML = '';
+
+ // If success server garuntees user object
+ if (body.login.success)
+ {
+ sessionStorage.setItem('user', JSON.stringify(body.login.user));
+ console.log(sessionStorage.user)
+ ClientID.innerHTML = `ClientID: ${JSON.parse(sessionStorage.user).uid}`;
+ }
}
diff --git a/client/public/main.css b/client/public/main.css
new file mode 100644
index 0000000..7542451
--- /dev/null
+++ b/client/public/main.css
@@ -0,0 +1,3 @@
+.red {
+ color: red;
+}
diff --git a/server/src/game-registrar.js b/server/src/game-registrar.js
index a7e1537..0566283 100644
--- a/server/src/game-registrar.js
+++ b/server/src/game-registrar.js
@@ -35,9 +35,17 @@ function CountIPs(ip)
return count;
}
+function ValidUsername(username)
+{
+ if (username.match(/[^A-Za-z0-9_-]/)) {
+ return false;
+ }
+ return true;
+}
+
function RegisterPlayer(username, ip)
{
- // TODO: Don't assume this is unique, even with Crypto
+ // TODO: Don't assume this is unique, even with Crypto, UUIDv4?
const id = Crypto.randomBytes(32).toString("hex");
OnlinePlayers[id] = {
@@ -57,5 +65,6 @@ module.exports = {
CheckUsernameAvailability: CheckUsernameAvailability,
CountIPs: CountIPs,
+ ValidUsername: ValidUsername,
RegisterPlayer: RegisterPlayer
}
diff --git a/server/src/router.js b/server/src/router.js
index 409debf..11d7dca 100644
--- a/server/src/router.js
+++ b/server/src/router.js
@@ -69,7 +69,7 @@ class Error
get toString()
{
- return JSON.stringify(this.toErrorObject);
+ return JSON.stringify({errors: this.toErrorObject});
}
get code()
@@ -91,9 +91,8 @@ RESPONDS
login:
{
success: true,
- userid: uid,
+ user: {userobject},
}
- errors: []
}
*/
function HandleLogin(req, res, next)
@@ -109,6 +108,13 @@ function HandleLogin(req, res, next)
const username = req.body.username;
+ if (!Game.Registrar.ValidUsername(username))
+ {
+ err.addError(403, 'Forbidden', 'Invalid username');
+ err.end(res);
+ return;
+ }
+
if (!Game.Registrar.CheckUsernameAvailability(username))
{
err.addError(403, 'Forbidden', 'Username taken');
@@ -129,9 +135,16 @@ function HandleLogin(req, res, next)
return;
}
- Game.Registrar.RegisterPlayer(username, ip);
+ const user = Game.Registrar.RegisterPlayer(username, ip);
- res.end(JSON.stringify(req.body.username));
+ const response = {
+ login: {
+ success: true,
+ user: user
+ }
+ }
+
+ res.end(JSON.stringify(response));
// Continue to later middleware
next();