]> code.citadel.org Git - citadel.git/blob - webcit-ng/static/js/login.js
2d8a510fa94159388637cb15a364d89835fd5628
[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-card-4 w3-animate-zoom" style="max-width:600px">
20         
21                         <div class="w3-center"><br>
22                                 <span class="w3-button w3-xlarge w3-transparent w3-display-topright" title="Close Modal">×</span>
23                                 <span class="w3-circle w3-margin-top"><i class="fa fa-user"></i></span>
24                         </div>
25
26                         <div class="w3-container">
27                                 <div class="w3-section">
28                                         <span class="w3-center w3-red">${any_message}</span>
29                                 </div>
30                                 <div class="w3-section">
31                                         <label><b>${_("User name:")}</b></label>
32                                         <input class="w3-input w3-border w3-margin-bottom" type="text" id="username" required>
33                                         <label><b>${_("Password:")}</b></label>
34                                         <input class="w3-input w3-border" type="password" id="password" required>
35                                         <button class="w3-button w3-block w3-blue w3-section w3-padding" onClick="javascript:login_button()">${_("Log in")}</button>
36                                         <input class="w3-check w3-margin-top" type="checkbox" checked="checked"> Remember me
37                                 </div>
38                         </div>
39
40                         <div class="w3-container w3-border-top w3-padding-16 w3-light-grey">
41                                 <button type="button" class="w3-button w3-red">Cancel</button>
42                                 <span class="w3-right w3-padding w3-hide-small">Forgot <a href="#">password?</a></span>
43                         </div>
44                 </div>
45         `;
46
47         document.getElementById("ctdl_big_modal").style.display = "block";
48 }
49
50
51 // When the user elects to log out, we just call /ctdl/a/logout and let the system flush the session.
52 // When we go back to ctdl_startup() it will detect that we are no longer logged in, and do the right thing.
53 //function logout() {
54 logout = async() => {
55         response = await fetch("/ctdl/a/logout");
56         ctdl_startup();                                 // let the regular startup code take care of everything else
57 }
58
59
60 function login_button(username) {
61         parms = 
62                 document.getElementById("username").value
63                 + "|"
64                 + document.getElementById("password").value
65                 + "|"
66         ;
67
68         var request = new XMLHttpRequest();
69         request.open("POST", "/ctdl/a/login", true);
70         request.onreadystatechange = function() {
71                 if (this.readyState === XMLHttpRequest.DONE) {
72                         login_result(JSON.parse(this.responseText));
73                 }
74         };
75         request.send(parms);
76         request = null;
77 }
78
79
80 // Feed this a JSON output from login_button() or a similar function
81 function login_result(data) {
82         if (data.result) {
83                 document.getElementById("ctdl_big_modal").style.display = "none";
84                 ctdl_startup();                         // let the regular startup code take care of everything else
85         }
86         else {
87                 display_login_screen(data.message);
88         }
89 }
90
91
92 // Detect whether the Citadel session is logged in as a user and update our internal variables accordingly.
93 function detect_logged_in() {
94         try {
95                 wcauth_decoded = atob(getCookieValue("wcauth"));
96                 wcauth_user = wcauth_decoded.split(":")[0];
97         }
98         catch(err) {
99                 wcauth_user = "";
100         }
101         if (wcauth_user.length > 0) {
102                 logged_in = 1;
103                 current_user = wcauth_user;
104         }
105         else {
106                 logged_in = 0;
107                 current_user = _("Not logged in.");
108         }
109 }