userpage: show and allow modification of contact methods
This commit is contained in:
+21
-4
@@ -1,17 +1,17 @@
|
||||
import { Modal } from "../modules/modal.js";
|
||||
import { toggleLoader } from "../modules/common.js";
|
||||
import { toggleLoader, _post } from "../modules/common.js";
|
||||
|
||||
export class Login {
|
||||
private _modal: Modal;
|
||||
private _form: HTMLFormElement;
|
||||
private _url: string;
|
||||
private _onLogin: (username: string, password: string) => void;
|
||||
private _logoutButton: HTMLElement = null;
|
||||
|
||||
constructor(modal: Modal, endpoint: string) {
|
||||
|
||||
this._url = window.URLBase + endpoint;
|
||||
if (this._url[this._url.length-1] != '/') this._url += "/";
|
||||
this._url += "token/";
|
||||
|
||||
this._modal = modal;
|
||||
this._form = this._modal.asElement().querySelector(".form-login") as HTMLFormElement;
|
||||
@@ -29,6 +29,18 @@ export class Login {
|
||||
};
|
||||
}
|
||||
|
||||
bindLogout = (button: HTMLElement) => {
|
||||
this._logoutButton = button;
|
||||
this._logoutButton.classList.add("unfocused");
|
||||
this._logoutButton.onclick = () => _post(this._url + "logout", null, (req: XMLHttpRequest): boolean => {
|
||||
if (req.readyState == 4 && req.status == 200) {
|
||||
window.token = "";
|
||||
location.reload();
|
||||
return false;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
get onLogin() { return this._onLogin; }
|
||||
set onLogin(f: (username: string, password: string) => void) { this._onLogin = f; }
|
||||
|
||||
@@ -36,7 +48,7 @@ export class Login {
|
||||
const req = new XMLHttpRequest();
|
||||
req.responseType = 'json';
|
||||
const refresh = (username == "" && password == "");
|
||||
req.open("GET", this._url + (refresh ? "refresh" : "login"), true);
|
||||
req.open("GET", this._url + (refresh ? "token/refresh" : "token/login"), true);
|
||||
if (!refresh) {
|
||||
req.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
|
||||
}
|
||||
@@ -46,6 +58,10 @@ export class Login {
|
||||
let errorMsg = window.lang.notif("errorConnection");
|
||||
if (req.response) {
|
||||
errorMsg = req.response["error"];
|
||||
const langErrorMsg = window.lang.strings(errorMsg);
|
||||
if (langErrorMsg) {
|
||||
errorMsg = langErrorMsg;
|
||||
}
|
||||
}
|
||||
if (!errorMsg) {
|
||||
errorMsg = window.lang.notif("errorUnknown");
|
||||
@@ -62,7 +78,8 @@ export class Login {
|
||||
this._onLogin(username, password);
|
||||
}
|
||||
this._modal.close();
|
||||
document.getElementById("logout-button").classList.remove("unfocused");
|
||||
if (this._logoutButton != null)
|
||||
this._logoutButton.classList.remove("unfocused");
|
||||
}
|
||||
if (run) { run(+req.status); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user