user login

This commit is contained in:
Ben Kyd
2021-01-30 20:12:55 +00:00
parent a4879e963a
commit 7361383ec9
5 changed files with 56 additions and 8 deletions

View File

@@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="./main.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bruh online</title>
</head>
@@ -13,7 +14,7 @@
<button type="submit">Submit</button>
</form>
<div id="client-id">awaiting client id</div>
<div id="client-id">Awaiting ClientID</div>
<div id="connection-state"></div>
<script src="index.js"></script>

View File

@@ -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}`;
}
}

3
client/public/main.css Normal file
View File

@@ -0,0 +1,3 @@
.red {
color: red;
}

View File

@@ -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
}

View File

@@ -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();