2023-06-15 22:52:16 +00:00
|
|
|
import { Modal } from "../modules/modal.js";
|
2023-06-17 16:26:36 +00:00
|
|
|
import { toggleLoader, _post } from "../modules/common.js";
|
2023-06-15 22:52:16 +00:00
|
|
|
|
|
|
|
export class Login {
|
|
|
|
private _modal: Modal;
|
|
|
|
private _form: HTMLFormElement;
|
|
|
|
private _url: string;
|
2023-06-26 19:28:20 +00:00
|
|
|
private _endpoint: string;
|
2023-06-15 22:52:16 +00:00
|
|
|
private _onLogin: (username: string, password: string) => void;
|
2023-06-17 16:26:36 +00:00
|
|
|
private _logoutButton: HTMLElement = null;
|
2023-06-15 22:52:16 +00:00
|
|
|
|
|
|
|
constructor(modal: Modal, endpoint: string) {
|
2023-06-26 19:28:20 +00:00
|
|
|
this._endpoint = endpoint;
|
2023-06-15 22:52:16 +00:00
|
|
|
this._url = window.URLBase + endpoint;
|
|
|
|
if (this._url[this._url.length-1] != '/') this._url += "/";
|
|
|
|
|
|
|
|
this._modal = modal;
|
|
|
|
this._form = this._modal.asElement().querySelector(".form-login") as HTMLFormElement;
|
|
|
|
this._form.onsubmit = (event: SubmitEvent) => {
|
|
|
|
event.preventDefault();
|
|
|
|
const button = (event.target as HTMLElement).querySelector(".submit") as HTMLSpanElement;
|
|
|
|
const username = (document.getElementById("login-user") as HTMLInputElement).value;
|
|
|
|
const password = (document.getElementById("login-password") as HTMLInputElement).value;
|
|
|
|
if (!username || !password) {
|
|
|
|
window.notifications.customError("loginError", window.lang.notif("errorLoginBlank"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
toggleLoader(button);
|
|
|
|
this.login(username, password, () => toggleLoader(button));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-17 16:26:36 +00:00
|
|
|
bindLogout = (button: HTMLElement) => {
|
|
|
|
this._logoutButton = button;
|
|
|
|
this._logoutButton.classList.add("unfocused");
|
2023-06-26 19:28:20 +00:00
|
|
|
const logoutFunc = (url: string, tryAgain: boolean) => {
|
|
|
|
_post(url + "logout", null, (req: XMLHttpRequest): boolean => {
|
|
|
|
if (req.readyState == 4 && req.status == 200) {
|
|
|
|
window.token = "";
|
|
|
|
location.reload();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}, false, (req: XMLHttpRequest) => {
|
|
|
|
if (req.readyState == 4 && req.status == 404 && tryAgain) {
|
|
|
|
console.log("trying without URL Base...");
|
|
|
|
logoutFunc(this._endpoint, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
this._logoutButton.onclick = () => logoutFunc(this._url, true);
|
2023-06-17 16:26:36 +00:00
|
|
|
};
|
|
|
|
|
2023-06-15 22:52:16 +00:00
|
|
|
get onLogin() { return this._onLogin; }
|
|
|
|
set onLogin(f: (username: string, password: string) => void) { this._onLogin = f; }
|
|
|
|
|
|
|
|
login = (username: string, password: string, run?: (state?: number) => void) => {
|
|
|
|
const req = new XMLHttpRequest();
|
|
|
|
req.responseType = 'json';
|
|
|
|
const refresh = (username == "" && password == "");
|
2023-06-17 16:26:36 +00:00
|
|
|
req.open("GET", this._url + (refresh ? "token/refresh" : "token/login"), true);
|
2023-06-15 22:52:16 +00:00
|
|
|
if (!refresh) {
|
|
|
|
req.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
|
|
|
|
}
|
|
|
|
req.onreadystatechange = ((req: XMLHttpRequest, _: Event): any => {
|
|
|
|
if (req.readyState == 4) {
|
|
|
|
if (req.status != 200) {
|
|
|
|
let errorMsg = window.lang.notif("errorConnection");
|
|
|
|
if (req.response) {
|
|
|
|
errorMsg = req.response["error"];
|
2023-06-17 16:26:36 +00:00
|
|
|
const langErrorMsg = window.lang.strings(errorMsg);
|
|
|
|
if (langErrorMsg) {
|
|
|
|
errorMsg = langErrorMsg;
|
|
|
|
}
|
2023-06-15 22:52:16 +00:00
|
|
|
}
|
|
|
|
if (!errorMsg) {
|
|
|
|
errorMsg = window.lang.notif("errorUnknown");
|
|
|
|
}
|
|
|
|
if (!refresh) {
|
|
|
|
window.notifications.customError("loginError", errorMsg);
|
|
|
|
} else {
|
|
|
|
this._modal.show();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const data = req.response;
|
|
|
|
window.token = data["token"];
|
|
|
|
if (this._onLogin) {
|
|
|
|
this._onLogin(username, password);
|
|
|
|
}
|
|
|
|
this._modal.close();
|
2023-06-17 16:26:36 +00:00
|
|
|
if (this._logoutButton != null)
|
|
|
|
this._logoutButton.classList.remove("unfocused");
|
2023-06-15 22:52:16 +00:00
|
|
|
}
|
|
|
|
if (run) { run(+req.status); }
|
|
|
|
}
|
|
|
|
}).bind(this, req);
|
|
|
|
req.send();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|