Debian Codegraph @

public/js/login.js

raw symbols SCIP
1 /**
2 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
3 * Copyright (C) 2020-2025 Aleksey Andreev (liuch)
4 *
5 * Available at:
6 * https://github.com/liuch/dmarc-srg
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation, either version 3 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 class LoginDialog extends VerticalDialog {
22 constructor(params) {
23 super();
24 this._params = params || {};
25 this._params.buttons = [ "ok", "cancel" ];
26 this._params.title = "Authentication";
27 this._params.overlay_click = "ignore";
28 this._user = null;
29 this._pass = null;
30 }
31
32 remove() {
33 if (this._element) {
34 this._element.remove();
35 this._element = null;
36 }
37 }
38
39 _gen_content() {
40 if (!this._params.nousername) {
41 this._user = this._insert_input_row("User name", "text", "Enter your user name");
42 }
43 this._pass = this._insert_input_row("Password", "password", "Enter your password");
44 this.display_status("wait", "Enter your credentials");
45 }
46
47 _insert_input_row(text, type, placeholder) {
48 let inp = document.createElement("input");
49 inp.required = true;
50 inp.setAttribute("type", type);
51 inp.setAttribute("placeholder", placeholder);
52 super._insert_input_row(text, inp);
53 return inp;
54 }
55
56 _enable_elements(enable) {
57 this._buttons[0].disabled = !enable;
58 for (const el of this._element.querySelector("form").elements) {
59 el.disabled = !enable;
60 }
61 }
62
63 _submit() {
64 this._buttons[1].focus();
65 this._enable_elements(false);
66 let body = {};
67 if (!this._params.nousername) {
68 body.username = this._user.value;
69 }
70 body.password = this._pass.value;
71 if (this._params.nofetch) {
72 this._result = body;
73 this.hide();
74 return;
75 }
76 let hide = false;
77 this.display_status("wait", "Sending credentials to the server...");
78 window.fetch("login.php", {
79 method: "POST",
80 cache: "no-store",
81 headers: Object.assign(HTTP_HEADERS, HTTP_HEADERS_POST),
82 credentials: "same-origin",
83 body: JSON.stringify(body)
84 }).then(resp => {
85 if (!resp.ok) throw new Error("Failed to log in");
86 return resp.json();
87 }).then(data => {
88 Common.checkResult(data);
89 this._result = data;
90 Notification.add({ type: "info", text: data.message || "Successfully!", name: "auth" });
91 hide = true;
92 }).catch(err => {
93 this._pass.value = "";
94 Common.displayError(err);
95 this.display_status("error", err.message);
96 }).finally(() => {
97 this._enable_elements(true);
98 if (hide) {
99 this.hide();
100 } else {
101 this.focus();
102 }
103 });
104 }
105 }
106
107 LoginDialog.start = function (params) {
108 if (User.auth_type !== "base") {
109 params ||= {};
110 if (params.nousername === undefined) params.nousername = true;
111 }
112 let login = new LoginDialog(params);
113 document.getElementById("main-block").appendChild(login.element());
114 login.show().then(function(d) {
115 if (d) {
116 Router.go();
117 }
118 }).catch(function(err) {
119 Common.displayError(err);
120 }).finally(function() {
121 login.remove();
122 login = null;
123 });
124 };