]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/static/js/login.js
Grammar change in the license declaration.
[citadel.git] / webcit-ng / static / js / login.js
index e6ab91d2aa77273fb933ae17b8727d30f7210aa6..11592ee62fd7febad0e1a554960f8c6ac5ef0f8f 100644 (file)
@@ -1,43 +1,52 @@
+// Copyright (c) 2016-2023 by the citadel.org team
 //
-// Copyright (c) 2016-2017 by the citadel.org team
-//
-// This program is open source software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License version 3.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
+// This program is open source software.  Use, duplication, or
+// disclosure is subject to the GNU General Public License v3.
 
 
-function display_login_screen(any_message)
-{
-       document.getElementById("main").innerHTML =
-               "Put the login screen here, dummary<br><br>" +
-               any_message + "<br><br>" +
-               _("User name:") + "<input type=\"text\" id=\"username\"><br>" +
-               _("Password:") + "<input type=\"password\" id=\"password\"><br>" +
-               "<a href=\"javascript:login_button()\">" + _("Log in") + "</a>"
-       ;
+// This is where the login screen (modal) is displayed.
+// It appears in the "ctdl_big_modal" div which is defined in index.html and is used for several different modals.
+// If you want to change the look of the login dialog, this is where to change it.
+function display_login_screen(any_message) {
+       document.getElementById("ctdl_big_modal").innerHTML = `
 
-       update_banner();
-}
+               <div class="ctdl-modal-header">
+                               <span><i class="fa fa-user"></i></span>
+                               <span>${any_message}</span>
+               </div>
 
+               <div class="ctdl-login-screen-grid-container">
+                       <div class="ctdl-login-screen-grid-item"><label><b>${_("User name:")}</b></label></div>
+                       <div class="ctdl-login-screen-grid-item"><input type="text" id="username" required></div>
+                       <div class="ctdl-login-screen-grid-item"><label><b>${_("Password:")}</b></label></div>
+                       <div class="ctdl-login-screen-grid-item"><input type="password" id="password" required></div>
+                       <div class="ctdl-login-screen-grid-item"></div>
+                       <div class="ctdl-login-screen-grid-item">
+                               <input type="checkbox" checked="checked">
+                               Remember me
+                       </div>
+                       <div class="ctdl-login-screen-grid-item"></div>
+                       <div class="ctdl-login-screen-grid-item">
+                               <button onClick="javascript:login_button()">${_("Log in")}</button>
+                               <button type="button">Cancel</button>
+                       </div>
+               </div>
+       `;
 
-function logout()
-{
-       var request = new XMLHttpRequest();
-       request.open("GET", "/ctdl/a/logout", true);
-       request.onreadystatechange = function() {
-               login_result(this.responseText);
-       };
-       request.send();
-       request = null;
+       document.getElementById("ctdl_big_modal").style.display = "block";
 }
 
 
-function login_button(username)
-{
+// When the user elects to log out, we just call /ctdl/a/logout and let the system flush the session.
+// When we go back to ctdl_startup() it will detect that we are no longer logged in, and do the right thing.
+//function logout() {
+//logout = async() => {
+       //response = await fetch("/ctdl/a/logout");
+       //ctdl_startup();                                       // let the regular startup code take care of everything else
+//}
+
+
+function login_button(username) {
        parms = 
                document.getElementById("username").value
                + "|"
@@ -48,44 +57,39 @@ function login_button(username)
        var request = new XMLHttpRequest();
        request.open("POST", "/ctdl/a/login", true);
        request.onreadystatechange = function() {
-               login_result(this.responseText);
+               if (this.readyState === XMLHttpRequest.DONE) {
+                       login_result(JSON.parse(this.responseText));
+               }
        };
        request.send(parms);
        request = null;
 }
 
 
-function login_result(data)
-{
-       if (data.substring(0,1) == "2") {
-               logged_in = 1;
-               current_user = data.substring(4).split("|")[0];
-               update_banner();
-               document.getElementById("main").innerHTML = "FIXME ok we are logged in as " + current_user + " ... " ;
+// Feed this a JSON output from login_button() or a similar function
+function login_result(data) {
+       if (data.result) {
+               document.getElementById("ctdl_big_modal").style.display = "none";
+               ctdl_startup();                         // let the regular startup code take care of everything else
        }
        else {
-               display_login_screen(data.substring(4));
+               display_login_screen(data.message);
        }
 }
 
 
 // Detect whether the Citadel session is logged in as a user and update our internal variables accordingly.
-//
-function detect_logged_in()
-{
-       var request = new XMLHttpRequest();
-       request.open("GET", "/ctdl/a/whoami", true);
-       request.onreadystatechange = function() {
-               detect_logged_in_2(this.responseText);
-       };
-       request.send();
-       request = null;
-}
-function detect_logged_in_2(data)
-{
-       if (data.length > 0) {
+function detect_logged_in() {
+       try {
+               wcauth_decoded = atob(getCookieValue("wcauth"));
+               wcauth_user = wcauth_decoded.split(":")[0];
+       }
+       catch(err) {
+               wcauth_user = "";
+       }
+       if (wcauth_user.length > 0) {
                logged_in = 1;
-               current_user = data;
+               current_user = wcauth_user;
        }
        else {
                logged_in = 0;