A little more cosmetic work on the login modal.
[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 function display_login_screen(any_message) {
15
16         document.getElementById("ctdl_big_modal").innerHTML =
17                   "<div class=\"w3-modal-content\">"
18                 + "  <div class=\"w3-panel w3-border w3-border-blue w3-topbar w3-bottombar w3-leftbar w3-rightbar\"><center>"
19
20                 + "<p>Put the login screen here, dummy</p>"
21                 + "<p>" + any_message + "</p>"
22                 + "<table border=0><tr><td>"
23                 + _("User name:") + "</td><td><input type=\"text\" id=\"username\"></td></tr><tr><td>"
24                 + _("Password:") + "</td><td><input type=\"password\" id=\"password\"></td></tr></table><br>"
25                 + "<p>"
26                 + "<button class=\"w3-button w3-blue\" onClick=\"javascript:login_button()\">" + _("Log in") + "</button>"
27                 + "</p>"
28
29                 + "  </center></div>"
30                 + "</div>";
31         document.getElementById("ctdl_big_modal").style.display = "block";
32 }
33
34
35 function logout() {
36         var request = new XMLHttpRequest();
37         request.open("GET", "/ctdl/a/logout", true);
38         request.onreadystatechange = function() {
39                 login_result(this.responseText);
40         };
41         request.send();
42         request = null;
43 }
44
45
46 function login_button(username) {
47         parms = 
48                 document.getElementById("username").value
49                 + "|"
50                 + document.getElementById("password").value
51                 + "|"
52         ;
53
54         var request = new XMLHttpRequest();
55         request.open("POST", "/ctdl/a/login", true);
56         request.onreadystatechange = function() {
57                 login_result(this.responseText);
58         };
59         request.send(parms);
60         request = null;
61 }
62
63
64 function login_result(data) {
65         if (data.substring(0,1) == "2") {
66                 logged_in = 1;
67                 current_user = data.substring(4).split("|")[0];
68                 update_banner();
69                 document.getElementById("ctdl-main").innerHTML = "FIXME ok we are logged in as " + current_user + " ... " ;
70                 document.getElementById("ctdl_big_modal").style.display = "none";
71         }
72         else {
73                 display_login_screen(data.substring(4));
74         }
75 }
76
77
78 // Detect whether the Citadel session is logged in as a user and update our internal variables accordingly.
79 function detect_logged_in() {
80         try {
81                 wcauth_decoded = atob(getCookieValue("wcauth"));
82                 wcauth_user = wcauth_decoded.split(":")[0];
83         }
84         catch(err) {
85                 wcauth_user = "";
86         }
87         if (wcauth_user.length > 0) {
88                 logged_in = 1;
89                 current_user = wcauth_user;
90         }
91         else {
92                 logged_in = 0;
93                 current_user = _("Not logged in.");
94         }
95 }