]> code.citadel.org Git - citadel.git/blob - webcit-ng/static/js/login.js
This is a better version of detect_logged_in() for webcit-ng that
[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-container\">"
19
20                 + "Put the login screen here, dummy<br><br>"
21                 + any_message + "<br><br>"
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                 + "<a href=\"javascript:login_button()\">" + _("Log in") + "</a>"
26
27                 + "  </div>"
28                 + "</div>";
29         document.getElementById("ctdl_big_modal").style.display = "block";
30 }
31
32
33 function logout() {
34         var request = new XMLHttpRequest();
35         request.open("GET", "/ctdl/a/logout", true);
36         request.onreadystatechange = function() {
37                 login_result(this.responseText);
38         };
39         request.send();
40         request = null;
41 }
42
43
44 function login_button(username) {
45         parms = 
46                 document.getElementById("username").value
47                 + "|"
48                 + document.getElementById("password").value
49                 + "|"
50         ;
51
52         var request = new XMLHttpRequest();
53         request.open("POST", "/ctdl/a/login", true);
54         request.onreadystatechange = function() {
55                 login_result(this.responseText);
56         };
57         request.send(parms);
58         request = null;
59 }
60
61
62 function login_result(data) {
63         if (data.substring(0,1) == "2") {
64                 logged_in = 1;
65                 current_user = data.substring(4).split("|")[0];
66                 update_banner();
67                 document.getElementById("ctdl-main").innerHTML = "FIXME ok we are logged in as " + current_user + " ... " ;
68                 document.getElementById("ctdl_big_modal").style.display = "none";
69         }
70         else {
71                 display_login_screen(data.substring(4));
72         }
73 }
74
75
76 // Detect whether the Citadel session is logged in as a user and update our internal variables accordingly.
77 function detect_logged_in() {
78         try {
79                 wcauth_decoded = atob(getCookieValue("wcauth"));
80                 wcauth_user = wcauth_decoded.split(":")[0];
81         }
82         catch(err) {
83                 wcauth_user = "";
84         }
85         if (wcauth_user.length > 0) {
86                 logged_in = 1;
87                 current_user = wcauth_user;
88         }
89         else {
90                 logged_in = 0;
91                 current_user = _("Not logged in.");
92         }
93 }