2021-01-05 18:16:23 +00:00
|
|
|
import { Modal } from "./modules/modal.js";
|
2021-01-11 19:17:43 +00:00
|
|
|
import { _get, _post, toggleLoader } from "./modules/common.js";
|
2021-01-12 23:43:44 +00:00
|
|
|
import { loadLangSelector } from "./modules/lang.js";
|
2020-10-22 16:50:40 +00:00
|
|
|
|
|
|
|
interface formWindow extends Window {
|
|
|
|
validationStrings: pwValStrings;
|
2020-11-04 20:46:06 +00:00
|
|
|
invalidPassword: string;
|
2021-01-05 18:16:23 +00:00
|
|
|
modal: Modal;
|
2021-01-14 14:22:20 +00:00
|
|
|
code: string;
|
2020-10-22 16:50:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 22:00:30 +00:00
|
|
|
interface pwValString {
|
|
|
|
singular: string;
|
|
|
|
plural: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface pwValStrings {
|
2021-01-05 18:16:23 +00:00
|
|
|
length: pwValString;
|
|
|
|
uppercase: pwValString;
|
|
|
|
lowercase: pwValString;
|
|
|
|
number: pwValString;
|
|
|
|
special: pwValString;
|
|
|
|
[ type: string ]: pwValString;
|
2020-10-20 22:00:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 23:43:44 +00:00
|
|
|
loadLangSelector("form");
|
2021-01-11 19:17:43 +00:00
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
window.modal = new Modal(document.getElementById("modal-success"));
|
|
|
|
declare var window: formWindow;
|
|
|
|
|
2020-10-20 22:00:30 +00:00
|
|
|
var defaultPwValStrings: pwValStrings = {
|
|
|
|
length: {
|
|
|
|
singular: "Must have at least {n} character",
|
2021-01-07 20:16:16 +00:00
|
|
|
plural: "Must have at least {n} characters"
|
2020-10-20 22:00:30 +00:00
|
|
|
},
|
|
|
|
uppercase: {
|
|
|
|
singular: "Must have at least {n} uppercase character",
|
|
|
|
plural: "Must have at least {n} uppercase characters"
|
|
|
|
},
|
|
|
|
lowercase: {
|
|
|
|
singular: "Must have at least {n} lowercase character",
|
|
|
|
plural: "Must have at least {n} lowercase characters"
|
|
|
|
},
|
|
|
|
number: {
|
|
|
|
singular: "Must have at least {n} number",
|
|
|
|
plural: "Must have at least {n} numbers"
|
|
|
|
},
|
|
|
|
special: {
|
|
|
|
singular: "Must have at least {n} special character",
|
|
|
|
plural: "Must have at least {n} special characters"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
const form = document.getElementById("form-create") as HTMLFormElement;
|
|
|
|
const submitButton = form.querySelector("input[type=submit]") as HTMLInputElement;
|
|
|
|
const submitSpan = form.querySelector("span.submit") as HTMLSpanElement;
|
|
|
|
let usernameField = document.getElementById("create-username") as HTMLInputElement;
|
|
|
|
const emailField = document.getElementById("create-email") as HTMLInputElement;
|
|
|
|
if (!window.usernameEnabled) { usernameField.parentElement.remove(); usernameField = emailField; }
|
|
|
|
const passwordField = document.getElementById("create-password") as HTMLInputElement;
|
|
|
|
const rePasswordField = document.getElementById("create-reenter-password") as HTMLInputElement;
|
|
|
|
|
|
|
|
const checkPasswords = () => {
|
|
|
|
if (passwordField.value != rePasswordField.value) {
|
|
|
|
rePasswordField.setCustomValidity(window.invalidPassword);
|
|
|
|
submitButton.disabled = true;
|
|
|
|
submitSpan.setAttribute("disabled", "");
|
2020-10-20 22:33:32 +00:00
|
|
|
} else {
|
2021-01-05 18:16:23 +00:00
|
|
|
rePasswordField.setCustomValidity("");
|
|
|
|
submitButton.disabled = false;
|
|
|
|
submitSpan.removeAttribute("disabled");
|
2020-10-20 22:33:32 +00:00
|
|
|
}
|
|
|
|
};
|
2021-01-05 18:16:23 +00:00
|
|
|
rePasswordField.addEventListener("keyup", checkPasswords);
|
|
|
|
passwordField.addEventListener("keyup", checkPasswords);
|
2020-10-20 22:33:32 +00:00
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
interface respDTO {
|
|
|
|
[ type: string ]: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface sendDTO {
|
|
|
|
code: string;
|
|
|
|
email: string;
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const create = (event: SubmitEvent) => {
|
|
|
|
event.preventDefault();
|
|
|
|
toggleLoader(submitSpan);
|
|
|
|
let send: sendDTO = {
|
2021-01-14 14:22:20 +00:00
|
|
|
code: window.code,
|
2021-01-05 18:16:23 +00:00
|
|
|
username: usernameField.value,
|
|
|
|
email: emailField.value,
|
|
|
|
password: passwordField.value
|
|
|
|
};
|
|
|
|
_post("/newUser", send, (req: XMLHttpRequest) => {
|
|
|
|
if (req.readyState == 4) {
|
|
|
|
let vals = JSON.parse(req.response) as respDTO;
|
|
|
|
let valid = true;
|
|
|
|
for (let type in vals) {
|
|
|
|
if (requirements[type]) { requirements[type].valid = vals[type]; }
|
|
|
|
if (!vals[type]) { valid = false; }
|
|
|
|
}
|
|
|
|
toggleLoader(submitSpan);
|
|
|
|
if (req.status == 200 && valid) {
|
|
|
|
window.modal.show();
|
|
|
|
} else {
|
|
|
|
submitSpan.classList.add("~critical");
|
|
|
|
submitSpan.classList.remove("~urge");
|
|
|
|
setTimeout(() => {
|
|
|
|
submitSpan.classList.add("~urge");
|
|
|
|
submitSpan.classList.remove("~critical");
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
form.onsubmit = create;
|
|
|
|
|
|
|
|
class Requirement {
|
|
|
|
private _name: string;
|
2021-01-09 01:00:27 +00:00
|
|
|
protected _minCount: number;
|
2021-01-05 18:16:23 +00:00
|
|
|
private _content: HTMLSpanElement;
|
|
|
|
private _valid: HTMLSpanElement;
|
|
|
|
private _li: HTMLLIElement;
|
|
|
|
|
|
|
|
get valid(): boolean { return this._valid.classList.contains("~positive"); }
|
|
|
|
set valid(state: boolean) {
|
|
|
|
if (state) {
|
|
|
|
this._valid.classList.add("~positive");
|
|
|
|
this._valid.classList.remove("~critical");
|
|
|
|
this._valid.innerHTML = `<i class="icon ri-check-line" title="valid"></i>`;
|
|
|
|
} else {
|
|
|
|
this._valid.classList.add("~critical");
|
|
|
|
this._valid.classList.remove("~positive");
|
|
|
|
this._valid.innerHTML = `<i class="icon ri-close-line" title="invalid"></i>`;
|
|
|
|
}
|
2020-10-20 22:00:30 +00:00
|
|
|
}
|
2021-01-05 18:16:23 +00:00
|
|
|
|
|
|
|
constructor(name: string, el: HTMLLIElement) {
|
|
|
|
this._name = name;
|
|
|
|
this._li = el;
|
|
|
|
this._content = this._li.querySelector("span.requirement-content") as HTMLSpanElement;
|
|
|
|
this._valid = this._li.querySelector("span.requirement-valid") as HTMLSpanElement;
|
|
|
|
this.valid = false;
|
|
|
|
this._minCount = +this._li.getAttribute("min");
|
|
|
|
|
2020-10-20 22:00:30 +00:00
|
|
|
let text = "";
|
2021-01-05 18:16:23 +00:00
|
|
|
if (this._minCount == 1) {
|
|
|
|
text = window.validationStrings[this._name].singular.replace("{n}", "1");
|
2020-10-20 22:00:30 +00:00
|
|
|
} else {
|
2021-01-05 18:16:23 +00:00
|
|
|
text = window.validationStrings[this._name].plural.replace("{n}", ""+this._minCount);
|
2020-10-20 22:00:30 +00:00
|
|
|
}
|
2021-01-05 18:16:23 +00:00
|
|
|
this._content.textContent = text;
|
2020-10-20 22:00:30 +00:00
|
|
|
}
|
2021-01-09 01:00:27 +00:00
|
|
|
|
|
|
|
validate = (count: number) => { this.valid = (count >= this._minCount); }
|
2020-10-20 22:00:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 01:00:27 +00:00
|
|
|
// Incredible code right here
|
|
|
|
const isInt = (s: string): boolean => { return (s in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]); }
|
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
const testStrings = (f: pwValString): boolean => {
|
|
|
|
const testString = (s: string): boolean => {
|
|
|
|
if (s == "" || !s.includes("{n}")) { return false; }
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return testString(f.singular) && testString(f.plural);
|
|
|
|
}
|
2020-10-20 22:00:30 +00:00
|
|
|
|
2021-01-09 01:00:27 +00:00
|
|
|
interface Validation { [name: string]: number }
|
|
|
|
|
|
|
|
const validate = (s: string): Validation => {
|
|
|
|
let v: Validation = {};
|
|
|
|
for (let criteria of ["length", "lowercase", "uppercase", "number", "special"]) { v[criteria] = 0; }
|
|
|
|
v["length"] = s.length;
|
|
|
|
for (let c of s) {
|
|
|
|
if (isInt(c)) { v["number"]++; }
|
|
|
|
else {
|
|
|
|
const upper = c.toUpperCase();
|
|
|
|
if (upper == c.toLowerCase()) { v["special"]++; }
|
|
|
|
else {
|
|
|
|
if (upper == c) { v["uppercase"]++; }
|
|
|
|
else if (upper != c) { v["lowercase"]++; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
passwordField.addEventListener("keyup", () => {
|
|
|
|
const v = validate(passwordField.value);
|
|
|
|
for (let criteria in requirements) {
|
|
|
|
requirements[criteria].validate(v[criteria]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
var requirements: { [category: string]: Requirement} = {};
|
2020-10-20 22:00:30 +00:00
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
if (!window.validationStrings) {
|
|
|
|
window.validationStrings = defaultPwValStrings;
|
|
|
|
} else {
|
|
|
|
for (let category in window.validationStrings) {
|
|
|
|
if (!testStrings(window.validationStrings[category])) {
|
|
|
|
window.validationStrings[category] = defaultPwValStrings[category];
|
2020-10-20 22:00:30 +00:00
|
|
|
}
|
2021-01-05 18:16:23 +00:00
|
|
|
const el = document.getElementById("requirement-" + category);
|
|
|
|
if (el) {
|
|
|
|
requirements[category] = new Requirement(category, el as HTMLLIElement);
|
2020-11-04 20:46:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|