Limit username characters.

This commit is contained in:
yukirij 2024-10-15 19:39:27 -07:00
parent 5196222d71
commit c3e2e3d5e1
4 changed files with 20 additions and 2 deletions

View File

@ -22,6 +22,7 @@ ring = "0.17.8"
const_format = "0.2.32"
markdown = "0.3.0"
usvg = "0.44.0"
regex = "1.11.0"
game = { path = "../game" }

View File

@ -142,9 +142,11 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
app.log.log("Request: Register");
let mut is_valid = true;
let regex = regex::Regex::new(r"^[\p{L}\p{N}\p{Pd}\p{Pc}\p{Sm}\p{Sc}]{1,24}$").unwrap();
let invite = app.invite_tokens.get(&request.code).cloned();
if is_valid && invite.is_none() { response.status = STATUS_BAD_CODE; is_valid = false; }
if is_valid && request.handle.len() == 0 && request.handle.chars().count() <= 24 { response.status = STATUS_BAD_HANDLE; is_valid = false; }
if is_valid && !regex.is_match(&request.handle) { response.status = STATUS_BAD_HANDLE; is_valid = false; }
if is_valid && request.secret.len() == 0 { response.status = STATUS_BAD_SECRET; is_valid = false; }
if is_valid {

View File

@ -45,6 +45,14 @@ const SCENES = {
let tb_handle = UI.textbox("handle", "user");
tb_handle.setAttribute("maxlength", 24);
tb_handle.addEventListener("input", (event) => {
let value = event.target.value;
if(value.length == 0 || VALID.username(value)) {
event.target.removeAttribute("class");
} else {
event.target.setAttribute("class", "error");
}
})
form.appendChild(UI.table(null, [
[ UI.label(LANG("handle"), "handle"), tb_handle ],
@ -71,7 +79,7 @@ const SCENES = {
let invitation = code.value;
invitation = invitation.replace(/\-/g, '');
if(handle.value.length > 0 && handle.value.length < 24 && secret.value.length > 0 && invitation.length > 0) {
if(VALID.username(handle.value) && secret.value.length > 0 && invitation.length > 0) {
event.target.setAttribute("disabled", "");
let enc = new TextEncoder();

View File

@ -283,3 +283,10 @@ const HEX = {
return (hex.x >= 0 && hex.x < 3 && hex.y >= COLUMNS[hex.x].x && hex.y <= COLUMNS[hex.x].y);
},
};
const VALID = {
username(text) {
const reg = /^[\p{L}\p{N}\p{Pd}\p{Pc}\p{Sm}\p{Sc}]{1,24}$/u;
return reg.test(text);
},
};