This is a better version of detect_logged_in() for webcit-ng that
[citadel.git] / webcit-ng / static / js / login.js
index 20e5de5f120d903e104e6e6c8c90d2c1aeb42084..9b8e425f58ed3c1220b27b83092ce7b833efe7a7 100644 (file)
@@ -1,5 +1,4 @@
-//
-// Copyright (c) 2016-2020 by the citadel.org team
+// Copyright (c) 2016-2022 by the citadel.org team
 //
 // This program is open source software.  It runs great on the
 // Linux operating system (and probably elsewhere).  You can use,
 // GNU General Public License for more details.
 
 
-function display_login_screen(any_message)
-{
-       document.getElementById("ctdl-main").innerHTML =
-               "<center><br><br>Put the login screen here, dummary<br><br>" +
-               any_message + "<br><br>" +
-               "<table border=0><tr><td>" +
-               _("User name:") + "</td><td><input type=\"text\" id=\"username\"></td></tr><tr><td>" +
-               _("Password:") + "</td><td><input type=\"password\" id=\"password\"></td></tr></table><br>" +
-               "<a href=\"javascript:login_button()\">" + _("Log in") + "</a></center>"
-       ;
+function display_login_screen(any_message) {
+
+       document.getElementById("ctdl_big_modal").innerHTML =
+                 "<div class=\"w3-modal-content\">"
+               + "  <div class=\"w3-container\">"
+
+               + "Put the login screen here, dummy<br><br>"
+               + any_message + "<br><br>"
+               + "<table border=0><tr><td>"
+               + _("User name:") + "</td><td><input type=\"text\" id=\"username\"></td></tr><tr><td>"
+               + _("Password:") + "</td><td><input type=\"password\" id=\"password\"></td></tr></table><br>"
+               + "<a href=\"javascript:login_button()\">" + _("Log in") + "</a>"
 
-       update_banner();
+               + "  </div>"
+               + "</div>";
+       document.getElementById("ctdl_big_modal").style.display = "block";
 }
 
 
-function logout()
-{
+function logout() {
        var request = new XMLHttpRequest();
        request.open("GET", "/ctdl/a/logout", true);
        request.onreadystatechange = function() {
@@ -39,8 +41,7 @@ function logout()
 }
 
 
-function login_button(username)
-{
+function login_button(username) {
        parms = 
                document.getElementById("username").value
                + "|"
@@ -58,13 +59,13 @@ function login_button(username)
 }
 
 
-function login_result(data)
-{
+function login_result(data) {
        if (data.substring(0,1) == "2") {
                logged_in = 1;
                current_user = data.substring(4).split("|")[0];
                update_banner();
                document.getElementById("ctdl-main").innerHTML = "FIXME ok we are logged in as " + current_user + " ... " ;
+               document.getElementById("ctdl_big_modal").style.display = "none";
        }
        else {
                display_login_screen(data.substring(4));
@@ -73,22 +74,17 @@ function login_result(data)
 
 
 // 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;