about summary refs log tree commit diff stats
path: root/src/app.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/app.js')
-rw-r--r--src/app.js46
1 files changed, 38 insertions, 8 deletions
diff --git a/src/app.js b/src/app.js
index 095df1c..3734e11 100644
--- a/src/app.js
+++ b/src/app.js
@@ -33,10 +33,10 @@ class AppCommon {
     urlParam(name){
         try {
             const results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
-            if (!results) return 0;
-            return decodeURIComponent(results[1].replace(/\+/g, " ")) || 0;
+            if (!results) return "";
+            return decodeURIComponent(results[1].replace(/\+/g, " ")) || "";
         } catch (e) {
-            return 0;
+            return "";
         }
     }
 
@@ -133,6 +133,35 @@ class AppClass extends AppCommon {
     async init() {
         console.log('version', this.version, 'current, worker', await this.getActiveCacheVersion());
 
+        // Read URL params for mode/query/offset
+        this.index_mode = this.urlParam("mode") || "";
+        this.index_query = this.urlParam("query") || "";
+        const offsetParam = parseInt(this.urlParam("offset"));
+        this.index_offset = isNaN(offsetParam) ? 0 : offsetParam;
+
+        // Update navbar active states
+        if (this.index_mode === "favorites") {
+            $(".index_favorites").addClass("active");
+            $(".download_all_link").show();
+            $(".logout_link").hide();
+        } else {
+            $(".index_all").addClass("active");
+            $(".download_all_link").hide();
+        }
+
+        $(".search_query").val(this.index_query);
+
+        // Auth check: call backend with a minimal request; 401 means unauthorized
+        try {
+            const authParams = new URLSearchParams({op: "list", limit: "1"});
+            await $.get("backend.php?" + authParams.toString());
+            // If we get here without error, we're authenticated
+        } catch (e) {
+            // 401 Unauthorized — redirect to login
+            window.location.href = "login.php";
+            return;
+        }
+
         this.csrf_token = Cookie.get('epube_csrf_token');
 
         setupCsrfPrefilter(this.csrf_token);
@@ -143,7 +172,7 @@ class AppClass extends AppCommon {
             $(".separate-search").show();
             $("body").addClass("is-epube-app");
 
-            if (this.urlParam("mode") == "favorites")
+            if (this.index_mode == "favorites")
                 EpubeApp.setPage("PAGE_FAVORITES");
             else
                 EpubeApp.setPage("PAGE_LIBRARY");
@@ -348,10 +377,7 @@ class AppClass extends AppCommon {
         this.initNightMode();
         this.initCommonEventHandlers();
 
-        const query = this.urlParam("query");
-
-        if (query)
-            $(".search_query").val(query);
+        $(".search_query").val(this.urlParam("query"));
 
         this.populateOfflineList();
     }
@@ -578,6 +604,10 @@ class AppClass extends AppCommon {
             }
 
             this.updateIndexPagination(data);
+        }).fail((xhr) => {
+            if (xhr.status === 401) {
+                window.location.href = "login.php";
+            }
         });
     }