This is a better version of detect_logged_in() for webcit-ng that
authorArt Cancro <ajc@citadel.org>
Fri, 14 Jan 2022 16:08:55 +0000 (11:08 -0500)
committerArt Cancro <ajc@citadel.org>
Fri, 14 Jan 2022 16:08:55 +0000 (11:08 -0500)
checks the server cookie instead of clumsily attempting a command.
This works synchronously so it can be combined with other commands
without creating a race condition.  Also moved the login screen to
a modal.

webcit-ng/ctdlclient.c
webcit-ng/http.c
webcit-ng/static/index.html
webcit-ng/static/js/login.js
webcit-ng/static/js/main.js
webcit-ng/static/js/util.js
webcit/listsub.c

index d44270d64314ea599daa679991f4f2dcff62adc7..3ef786707be723c70f0a917c71c97ca43f2206a5 100644 (file)
@@ -57,12 +57,12 @@ int ctdl_readline(struct ctdlsession *ctdl, char *buf, int maxbytes) {
                                --len;
                        }
                        buf[len] = 0;
-                       // syslog(LOG_DEBUG, "\033[33m[ %s\033[0m", buf);
+                       syslog(LOG_DEBUG, "\033[32m[ %s\033[0m", buf);
                        return (len);
                }
                ++len;
        }
-       // syslog(LOG_DEBUG, "\033[33m[ %s\033[0m", buf);
+       syslog(LOG_DEBUG, "\033[32m[ %s\033[0m", buf);
        return (len);
 }
 
index f39ebe2146a1e2dad4eaa5ea1ec28f2affe3236f..a16e7c5af7ac99122aaef7ae34631a87463e0f13 100644 (file)
@@ -229,7 +229,7 @@ void perform_one_http_transaction(struct client_handle *ch) {
                #endif
 
                // Output the results back to the client.
-               syslog(LOG_DEBUG, "> %03d %s", h.response_code, h.response_string);
+               syslog(LOG_DEBUG, "\033[33m\033[1m> %03d %s\033[0m", h.response_code, h.response_string);
                client_printf(ch, "HTTP/1.1 %03d %s\r\n", h.response_code, h.response_string);
                client_printf(ch, "Connection: close\r\n");
                client_printf(ch, "Content-Length: %ld\r\n", h.response_body_length);
index af8aab6767183dbea83a682460eb06466e6c4b35..4e1f71b3acd1c787dd54e100aef82021c53fbaf4 100644 (file)
 
 <!-- Modal dialog (when needed) -->
 <div id="ctdl_big_modal" class="w3-modal" style="display:none; z-index:5">
-       <div class="w3-modal-content">
-               <div id="ctdl_big_modal_content" class="w3-container">
-                       <p>Loading...</p>
-               </div>
-       </div>
+LOADING
 </div>
 
 <!-- Top container -->
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;
index 615bdaaf4bdf1cbc91cac197f314aecdcf43ee04..77779f075ed7b4d3fe1b9a9486e6da3a451a26ab 100644 (file)
@@ -1,4 +1,3 @@
-//
 // Copyright (c) 2016-2020 by the citadel.org team
 //
 // This program is open source software.  It runs great on the
@@ -44,9 +43,15 @@ ctdl_startup = async() => {
 
                update_banner();
 
-               // for now, show a room list in the main div
-               gotoroom("_BASEROOM_");
-               display_room_list();
+               // What do we do upon landing?
+
+               if ( (serv_info.serv_supports_guest) || (logged_in) ) {                 // If the Lobby is visible,
+                       gotoroom("_BASEROOM_");                                         // go there.
+                       display_room_list();
+               }
+               else {                                                                  // Otherwise,
+                       display_login_screen("logged in users only.  sheeeeeeeeeit.");  // display the login modal.
+               }
        }
        else {
                document.getElementById("ctdl-main").innerHTML =
@@ -131,13 +136,12 @@ function update_banner() {
 
 
 // goto room
-//
 function gotoroom(roomname) {
 
        fetch_room = async() => {
                response = await fetch("/ctdl/r/" + escapeHTMLURI(roomname) + "/");
-               data = await(response.json());
                if (response.ok) {
+                       data = await(response.json());
                        current_room = data.name;
                        new_messages = data.new_messages;
                        total_messages = data.total_messages;
@@ -198,9 +202,15 @@ function load_new_march_list() {
 
 // Activate the "Loading..." modal
 function activate_loading_modal() {
-       document.getElementById("ctdl_big_modal_content").innerHTML =
-               "<i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
-               + _("Loading messages from server, please wait");
+       document.getElementById("ctdl_big_modal").innerHTML =
+                 "<div class=\"w3-modal-content\">"
+               + "  <div class=\"w3-container\">"
+
+               + "<i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
+               + _("Loading messages from server, please wait")
+
+               + "  </div>"
+               + "</div>";
        document.getElementById("ctdl_big_modal").style.display = "block";
 }
 
index 201247450aa720bd6803710bcfb424ce42046597..2499ca759bc1f053da5b5f9b5a98759fdfd57400 100644 (file)
@@ -114,3 +114,12 @@ function convertTimestamp(timestamp) {
                
        return time;
 }
+
+
+// Get the value of a cookie from the HTTP session
+// Shamelessly swiped from https://stackoverflow.com/questions/5639346/what-is-the-shortest-function-for-reading-a-cookie-by-name-in-javascript
+const getCookieValue = (name) => (
+       document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop() || ''
+)
+
+
index 6236f4b360bc7c5481e0fc0e3f572eac39280957..95f9ecfdbf47344ef60955bf0fa09cfc21c8da05 100644 (file)
@@ -98,6 +98,7 @@ int confirm_sub_or_unsub(char *cmd, StrBuf *Target, WCTemplputParams *TP) {
        return rc == 2;
 }
 
+
 int Conditional_LISTSUB_EXECUTE_CONFIRMSUBSCRIBE(StrBuf *Target, WCTemplputParams *TP) {
        if (strcmp(bstr("cmd"), "confirm_subscribe")) {
                return 0;