]> code.citadel.org Git - citadel.git/blob - webcit-ng/static/js/login.js
Continuing along with the effort to replace the remaining w3css styles with hand...
[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.  Use, duplication, or
4 // disclosure are subject to the GNU General Public License v3.
5
6
7 // This is where the login screen (modal) is displayed.
8 // It appears in the "ctdl_big_modal" div which is defined in index.html and is used for several different modals.
9 // If you want to change the look of the login dialog, this is where to change it.
10 function display_login_screen(any_message) {
11         document.getElementById("ctdl_big_modal").innerHTML = `
12
13                 <div class="ctdl-modal-header">
14                                 <span><i class="fa fa-user"></i></span>
15                                 <span>${any_message}</span>
16                 </div>
17
18                 <div class="ctdl-modal-main">
19                         <label><b>${_("User name:")}</b></label>
20                         <input type="text" id="username" required>
21                         <br>
22                         <label><b>${_("Password:")}</b></label>
23                         <input type="password" id="password" required>
24                         <br>
25                         <input type="checkbox" checked="checked"> Remember me
26                 </div>
27
28                 <div class="ctdl-modal-footer">
29                         <button onClick="javascript:login_button()">${_("Log in")}</button>
30                         <button type="button">Cancel</button>
31                 </div>
32         `;
33
34         document.getElementById("ctdl_big_modal").style.display = "block";
35 }
36
37
38 // When the user elects to log out, we just call /ctdl/a/logout and let the system flush the session.
39 // When we go back to ctdl_startup() it will detect that we are no longer logged in, and do the right thing.
40 //function logout() {
41 logout = async() => {
42         response = await fetch("/ctdl/a/logout");
43         ctdl_startup();                                 // let the regular startup code take care of everything else
44 }
45
46
47 function login_button(username) {
48         parms = 
49                 document.getElementById("username").value
50                 + "|"
51                 + document.getElementById("password").value
52                 + "|"
53         ;
54
55         var request = new XMLHttpRequest();
56         request.open("POST", "/ctdl/a/login", true);
57         request.onreadystatechange = function() {
58                 if (this.readyState === XMLHttpRequest.DONE) {
59                         login_result(JSON.parse(this.responseText));
60                 }
61         };
62         request.send(parms);
63         request = null;
64 }
65
66
67 // Feed this a JSON output from login_button() or a similar function
68 function login_result(data) {
69         if (data.result) {
70                 document.getElementById("ctdl_big_modal").style.display = "none";
71                 ctdl_startup();                         // let the regular startup code take care of everything else
72         }
73         else {
74                 display_login_screen(data.message);
75         }
76 }
77
78
79 // Detect whether the Citadel session is logged in as a user and update our internal variables accordingly.
80 function detect_logged_in() {
81         try {
82                 wcauth_decoded = atob(getCookieValue("wcauth"));
83                 wcauth_user = wcauth_decoded.split(":")[0];
84         }
85         catch(err) {
86                 wcauth_user = "";
87         }
88         if (wcauth_user.length > 0) {
89                 logged_in = 1;
90                 current_user = wcauth_user;
91         }
92         else {
93                 logged_in = 0;
94                 current_user = _("Not logged in.");
95         }
96 }