the dreaded style cleanup
[citadel.git] / webcit-ng / static / js / login.js
1 //
2 // Copyright (c) 2016-2020 by the citadel.org team
3 //
4 // This program is open source software.  It runs great on the
5 // Linux operating system (and probably elsewhere).  You can use,
6 // copy, and run it under the terms of the GNU General Public
7 // License version 3.  Richard Stallman is an asshole communist.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13
14
15 function display_login_screen(any_message)
16 {
17         document.getElementById("ctdl-main").innerHTML =
18                 "<center><br><br>Put the login screen here, dummary<br><br>" +
19                 any_message + "<br><br>" +
20                 "<table border=0><tr><td>" +
21                 _("User name:") + "</td><td><input type=\"text\" id=\"username\"></td></tr><tr><td>" +
22                 _("Password:") + "</td><td><input type=\"password\" id=\"password\"></td></tr></table><br>" +
23                 "<a href=\"javascript:login_button()\">" + _("Log in") + "</a></center>"
24         ;
25
26         update_banner();
27 }
28
29
30 function logout()
31 {
32         var request = new XMLHttpRequest();
33         request.open("GET", "/ctdl/a/logout", true);
34         request.onreadystatechange = function() {
35                 login_result(this.responseText);
36         };
37         request.send();
38         request = null;
39 }
40
41
42 function login_button(username)
43 {
44         parms = 
45                 document.getElementById("username").value
46                 + "|"
47                 + document.getElementById("password").value
48                 + "|"
49         ;
50
51         var request = new XMLHttpRequest();
52         request.open("POST", "/ctdl/a/login", true);
53         request.onreadystatechange = function() {
54                 login_result(this.responseText);
55         };
56         request.send(parms);
57         request = null;
58 }
59
60
61 function login_result(data)
62 {
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         }
69         else {
70                 display_login_screen(data.substring(4));
71         }
72 }
73
74
75 // Detect whether the Citadel session is logged in as a user and update our internal variables accordingly.
76 //
77 function detect_logged_in()
78 {
79         var request = new XMLHttpRequest();
80         request.open("GET", "/ctdl/a/whoami", true);
81         request.onreadystatechange = function() {
82                 detect_logged_in_2(this.responseText);
83         };
84         request.send();
85         request = null;
86 }
87 function detect_logged_in_2(data)
88 {
89         if (data.length > 0) {
90                 logged_in = 1;
91                 current_user = data;
92         }
93         else {
94                 logged_in = 0;
95                 current_user = _("Not logged in.");
96         }
97 }