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

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