]> code.citadel.org Git - citadel.git/blob - webcit-ng/static/js/login.js
7f35b8ea39ef7d31bb8a97dc49e789ba6582f403
[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 w3-animate-zoom\">"
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         response = await fetch("/ctdl/a/logout");
40         ctdl_startup();                                 // let the regular startup code take care of everything else
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                 document.getElementById("ctdl_big_modal").style.display = "none";
65                 ctdl_startup();                         // let the regular startup code take care of everything else
66         }
67         else {
68                 display_login_screen(data.substring(4));
69         }
70 }
71
72
73 // Detect whether the Citadel session is logged in as a user and update our internal variables accordingly.
74 function detect_logged_in() {
75         try {
76                 wcauth_decoded = atob(getCookieValue("wcauth"));
77                 wcauth_user = wcauth_decoded.split(":")[0];
78         }
79         catch(err) {
80                 wcauth_user = "";
81         }
82         if (wcauth_user.length > 0) {
83                 logged_in = 1;
84                 current_user = wcauth_user;
85         }
86         else {
87                 logged_in = 0;
88                 current_user = _("Not logged in.");
89         }
90 }