From edf446fae669f3370609f5a955c3d6c863a93524 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 26 Jan 2025 22:00:21 +0200 Subject: feat: Replace sessionStorage with (custom) cookieStorage --- views/archive/index.php | 10 ---------- views/authenticate.js | 23 ++++++++++++++++++++++- views/global/router.php | 2 ++ views/login/index.php | 2 +- views/logout/index.php | 8 +++----- views/newlist/index.php | 7 +------ views/profile/authenticate.php | 3 +-- 7 files changed, 30 insertions(+), 25 deletions(-) (limited to 'views') diff --git a/views/archive/index.php b/views/archive/index.php index ac6ada6..7de2dff 100644 --- a/views/archive/index.php +++ b/views/archive/index.php @@ -18,13 +18,8 @@
-
- @@ -61,12 +56,7 @@

"" hasn't been archived yet!

-
- diff --git a/views/authenticate.js b/views/authenticate.js index 8b158ce..398794a 100644 --- a/views/authenticate.js +++ b/views/authenticate.js @@ -1,3 +1,25 @@ +var cookieStorage = { + getItem: function(index) { + let cookies = document.cookie.split(';'); + for (cookie of cookies) { + let values = cookie.trim().split('='); + if (values[0] === index) { + return values[1]; + } + } + return undefined; + }, + setItem: function(index, value, expires = 'Fri, 31 Dec 9999 23:59:59 GMT') { + let cookie = index + '=' + value + ';'; + cookie += 'expires=' + expires + ';'; + cookie += 'path=/'; + document.cookie = cookie; + }, + removeItem: function(index) { + cookieStorage.setItem(index, "", 'Thu, 01 Jan 1970 00:00:00 GMT'); + }, +}; + var authentication_response = null; var authentication_callbacks = []; @@ -9,7 +31,6 @@ function requestAuthentication() { authentication_response = (request.status == 200) ? request.responseText : ""; } request.open("POST", "/authenticate", true); - request.setRequestHeader("Authorization", sessionStorage.getItem("token")); request.send(null); } requestAuthentication(); diff --git a/views/global/router.php b/views/global/router.php index fa9348d..cd9b304 100644 --- a/views/global/router.php +++ b/views/global/router.php @@ -40,6 +40,8 @@ foreach (glob($MODELS_DIR . '/*.php') as $filename) { require_once $filename; } +$TOKEN = (array_key_exists('token', $_COOKIE)) ? ($_COOKIE['token'] ?? "") : (""); + if (str_ends_with($view, '.php')) { require_once $view; } diff --git a/views/login/index.php b/views/login/index.php index 94faafe..da0d8e2 100644 --- a/views/login/index.php +++ b/views/login/index.php @@ -10,7 +10,7 @@

diff --git a/views/logout/index.php b/views/logout/index.php index fc9af5a..87878ed 100644 --- a/views/logout/index.php +++ b/views/logout/index.php @@ -1,14 +1,11 @@

Logging you out...

diff --git a/views/newlist/index.php b/views/newlist/index.php index 289c9da..72ac7a3 100644 --- a/views/newlist/index.php +++ b/views/newlist/index.php @@ -1,5 +1,5 @@ @@ -19,10 +19,5 @@ - - diff --git a/views/profile/authenticate.php b/views/profile/authenticate.php index afe1ca7..e4cf47e 100644 --- a/views/profile/authenticate.php +++ b/views/profile/authenticate.php @@ -9,8 +9,7 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') { } try { - $headers = apache_request_headers(); - $user = Database\Cookie::fromDB($headers["Authorization"]); + $user = Database\Cookie::fromDB($TOKEN); http_response_code(200); header('Content-Type: text/plain'); -- cgit v1.2.3