Grammar change in the license declaration.
[citadel.git] / webcit-ng / static / js / login.js
1 // Copyright (c) 2016-2023 by the citadel.org team
2 //
3 // This program is open source software.  Use, duplication, or
4 // disclosure is 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-login-screen-grid-container">
19                         <div class="ctdl-login-screen-grid-item"><label><b>${_("User name:")}</b></label></div>
20                         <div class="ctdl-login-screen-grid-item"><input type="text" id="username" required></div>
21                         <div class="ctdl-login-screen-grid-item"><label><b>${_("Password:")}</b></label></div>
22                         <div class="ctdl-login-screen-grid-item"><input type="password" id="password" required></div>
23                         <div class="ctdl-login-screen-grid-item"></div>
24                         <div class="ctdl-login-screen-grid-item">
25                                 <input type="checkbox" checked="checked">
26                                 Remember me
27                         </div>
28                         <div class="ctdl-login-screen-grid-item"></div>
29                         <div class="ctdl-login-screen-grid-item">
30                                 <button onClick="javascript:login_button()">${_("Log in")}</button>
31                                 <button type="button">Cancel</button>
32                         </div>
33                 </div>
34         `;
35
36         document.getElementById("ctdl_big_modal").style.display = "block";
37 }
38
39
40 // When the user elects to log out, we just call /ctdl/a/logout and let the system flush the session.
41 // When we go back to ctdl_startup() it will detect that we are no longer logged in, and do the right thing.
42 //function logout() {
43 //logout = async() => {
44         //response = await fetch("/ctdl/a/logout");
45         //ctdl_startup();                                       // let the regular startup code take care of everything else
46 //}
47
48
49 function login_button(username) {
50         parms = 
51                 document.getElementById("username").value
52                 + "|"
53                 + document.getElementById("password").value
54                 + "|"
55         ;
56
57         var request = new XMLHttpRequest();
58         request.open("POST", "/ctdl/a/login", true);
59         request.onreadystatechange = function() {
60                 if (this.readyState === XMLHttpRequest.DONE) {
61                         login_result(JSON.parse(this.responseText));
62                 }
63         };
64         request.send(parms);
65         request = null;
66 }
67
68
69 // Feed this a JSON output from login_button() or a similar function
70 function login_result(data) {
71         if (data.result) {
72                 document.getElementById("ctdl_big_modal").style.display = "none";
73                 ctdl_startup();                         // let the regular startup code take care of everything else
74         }
75         else {
76                 display_login_screen(data.message);
77         }
78 }
79
80
81 // Detect whether the Citadel session is logged in as a user and update our internal variables accordingly.
82 function detect_logged_in() {
83         try {
84                 wcauth_decoded = atob(getCookieValue("wcauth"));
85                 wcauth_user = wcauth_decoded.split(":")[0];
86         }
87         catch(err) {
88                 wcauth_user = "";
89         }
90         if (wcauth_user.length > 0) {
91                 logged_in = 1;
92                 current_user = wcauth_user;
93         }
94         else {
95                 logged_in = 0;
96                 current_user = _("Not logged in.");
97         }
98 }