]> code.citadel.org Git - citadel.git/blob - webcit-ng/static/js/login.js
ok, that function needed to be async
[citadel.git] / webcit-ng / static / js / login.js
1 // Copyright (c) 2016-2022 by the citadel.org team
2 //
3 // This program is open source software.  It runs great on the
4 // Linux operating system (and probably elsewhere).  You can use,
5 // copy, and run it under the terms of the GNU General Public
6 // License version 3.  Richard Stallman is an asshole communist.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12
13
14 // This is where the login screen (modal) is displayed.
15 // It appears in the "ctdl_big_modal" div which is defined in index.html and is used for several different modals.
16 // If you want to change the look of the login dialog, this is where to change it.
17 function display_login_screen(any_message) {
18         document.getElementById("ctdl_big_modal").innerHTML =
19                   "<div class=\"w3-modal-content\">"
20                 + "<div class=\"w3-panel w3-border w3-border-blue w3-topbar w3-bottombar w3-leftbar w3-rightbar\"><center>"
21                 + "<p>FIXME put a login banner here, dummy</p>"
22                 + "<p>" + any_message + "</p>"
23                 + "<table border=0><tr><td>"
24                 + _("User name:") + "</td><td><input type=\"text\" id=\"username\"></td></tr><tr><td>"
25                 + _("Password:") + "</td><td><input type=\"password\" id=\"password\"></td></tr></table><br>"
26                 + "<p>"
27                 + "<button class=\"w3-button w3-blue\" onClick=\"javascript:login_button()\">" + _("Log in") + "</button>"
28                 + "</p>"
29                 + "</center></div>"
30                 + "</div>";
31         document.getElementById("ctdl_big_modal").style.display = "block";
32 }
33
34
35 // When the user elects to log out, we just call /ctdl/a/logout and let the system flush the session.
36 // When we go back to ctdl_startup() it will detect that we are no longer logged in, and do the right thing.
37 //function logout() {
38 logout = async() => {
39
40         response = await fetch("/ctdl/a/logout");
41         ctdl_startup();
42 }
43
44
45 function login_button(username) {
46         parms = 
47                 document.getElementById("username").value
48                 + "|"
49                 + document.getElementById("password").value
50                 + "|"
51         ;
52
53         var request = new XMLHttpRequest();
54         request.open("POST", "/ctdl/a/login", true);
55         request.onreadystatechange = function() {
56                 login_result(this.responseText);
57         };
58         request.send(parms);
59         request = null;
60 }
61
62
63 function login_result(data) {
64         if (data.substring(0,1) == "2") {
65                 logged_in = 1;
66                 current_user = data.substring(4).split("|")[0];
67                 update_banner();
68                 document.getElementById("ctdl-main").innerHTML = "FIXME ok we are logged in as " + current_user + " ... " ;
69                 document.getElementById("ctdl_big_modal").style.display = "none";
70         }
71         else {
72                 display_login_screen(data.substring(4));
73         }
74 }
75
76
77 // Detect whether the Citadel session is logged in as a user and update our internal variables accordingly.
78 function detect_logged_in() {
79         try {
80                 wcauth_decoded = atob(getCookieValue("wcauth"));
81                 wcauth_user = wcauth_decoded.split(":")[0];
82         }
83         catch(err) {
84                 wcauth_user = "";
85         }
86         if (wcauth_user.length > 0) {
87                 logged_in = 1;
88                 current_user = wcauth_user;
89         }
90         else {
91                 logged_in = 0;
92                 current_user = _("Not logged in.");
93         }
94 }