+ add validation server side

This commit is contained in:
Mittani
2024-12-31 13:06:43 -08:00
committed by evanpelle
parent aa01ea7694
commit aba8e5dd81
10 changed files with 166 additions and 36 deletions
+17 -2
View File
@@ -12,6 +12,7 @@ import { GamePhase, GameServer } from './GameServer';
import { archive } from './Archive';
import { DiscordBot } from './DiscordBot';
import {MAX_USERNAME_LENGTH, MIN_USERNAME_LENGTH} from "../core/Util";
import {validateUsername} from "../core/validations/username";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -148,12 +149,26 @@ wss.on('connection', (ws, req) => {
if (Array.isArray(ip)) {
ip = ip[0]
}
gm.addClient(
const username = clientMsg.username;
const { isValid, error } = validateUsername(username);
if (!isValid) {
const errorMsg = error || "Invalid username.";
// Send error back to the client
ws.send(JSON.stringify({
type: 'error',
input: 'username-input',
message: errorMsg,
}));
return;
}
// If username is valid, add the client
gm.addClient(
new Client(
clientMsg.clientID,
clientMsg.persistentID,
ip,
clientMsg.username,
username,
ws
),
clientMsg.gameID,