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