From 3e0137938fc6c9844ba684dc63cc1688ff58a733 Mon Sep 17 00:00:00 2001 From: "Felipe M." Date: Mon, 9 Dec 2024 14:14:27 +0100 Subject: [PATCH 1/5] fix: Validate session on login component mount to prevent unnecessary login form --- internal/view/assets/js/component/login.js | 27 ++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/internal/view/assets/js/component/login.js b/internal/view/assets/js/component/login.js index 9150f0ee9..00587a21b 100644 --- a/internal/view/assets/js/component/login.js +++ b/internal/view/assets/js/component/login.js @@ -128,8 +128,28 @@ export default { }); }, }, - mounted() { - // Clear any existing cookies + async mounted() { + // Check if there's a valid session first + const token = localStorage.getItem("shiori-token"); + if (token) { + try { + const response = await fetch(new URL("api/v1/auth/check", document.baseURI), { + headers: { + "Authorization": `Bearer ${token}` + } + }); + + if (response.ok) { + // Valid session exists, emit login success + this.$emit("login-success"); + return; + } + } catch (err) { + // Continue with login form if check fails + } + } + + // Clear session data if we reach here document.cookie = `session-id=; Path=${ new URL(document.baseURI).pathname }; Expires=Thu, 01 Jan 1970 00:00:00 GMT;`; @@ -137,11 +157,10 @@ export default { new URL(document.baseURI).pathname }; Expires=Thu, 01 Jan 1970 00:00:00 GMT;`; - // Clear local storage localStorage.removeItem("shiori-account"); localStorage.removeItem("shiori-token"); - // wasn't working all the time, so I'm putting this here as a fallback + // Focus username input this.$nextTick(() => { const usernameInput = document.querySelector("#username"); if (usernameInput) { From 5e6b7794ddbb0c5a6ea6366578e1ac00f0a30a00 Mon Sep 17 00:00:00 2001 From: "Felipe M." Date: Mon, 9 Dec 2024 14:17:58 +0100 Subject: [PATCH 2/5] fix: Replace non-existent `api/v1/auth/check` with `api/v1/auth/me` --- internal/view/assets/js/component/login.js | 2 +- internal/view/index.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/view/assets/js/component/login.js b/internal/view/assets/js/component/login.js index 00587a21b..7026bfaec 100644 --- a/internal/view/assets/js/component/login.js +++ b/internal/view/assets/js/component/login.js @@ -133,7 +133,7 @@ export default { const token = localStorage.getItem("shiori-token"); if (token) { try { - const response = await fetch(new URL("api/v1/auth/check", document.baseURI), { + const response = await fetch(new URL("api/v1/auth/me", document.baseURI), { headers: { "Authorization": `Bearer ${token}` } diff --git a/internal/view/index.html b/internal/view/index.html index e8f0be25d..bf70cc32c 100644 --- a/internal/view/index.html +++ b/internal/view/index.html @@ -169,7 +169,7 @@ } try { - const response = await fetch(new URL("api/v1/auth/check", document.baseURI), { + const response = await fetch(new URL("api/v1/auth/me", document.baseURI), { headers: { "Authorization": `Bearer ${token}` } From fe993cbd1e054d32eff5f34ae679ad7e9038176d Mon Sep 17 00:00:00 2001 From: "Felipe M." Date: Mon, 9 Dec 2024 14:18:44 +0100 Subject: [PATCH 3/5] feat: Prevent login form flickering by conditionally rendering only when needed --- internal/view/index.html | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/view/index.html b/internal/view/index.html index bf70cc32c..79cfe461f 100644 --- a/internal/view/index.html +++ b/internal/view/index.html @@ -22,7 +22,7 @@
- +
@@ -59,7 +59,8 @@ 'login-view': LoginComponent }, data: { - isLoggedIn: false, + isLoggedIn: null, + loginRequired: false, activePage: "page-home", sidebarItems: [{ title: "Home", @@ -197,6 +198,8 @@ if (isValid) { this.loadSetting(); this.loadAccount(); + } else { + this.loginRequired = true; } } }, From 711a9f67b110b48979abfbbd8497abd5f2e1a706 Mon Sep 17 00:00:00 2001 From: "Felipe M." Date: Mon, 9 Dec 2024 14:19:11 +0100 Subject: [PATCH 4/5] feat: Show login component after user logout --- internal/view/index.html | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/view/index.html b/internal/view/index.html index 79cfe461f..177cc542f 100644 --- a/internal/view/index.html +++ b/internal/view/index.html @@ -104,6 +104,7 @@ document.cookie = `session-id=; Path=${new URL(document.baseURI).pathname}; Expires=Thu, 01 Jan 1970 00:00:00 GMT;`; document.cookie = `token=; Path=${new URL(document.baseURI).pathname}; Expires=Thu, 01 Jan 1970 00:00:00 GMT;`; this.isLoggedIn = false; + this.loginRequired = true; }).catch(err => { this.dialog.loading = false; this.getErrorMessage(err).then(msg => { From aeb872d467570e962c5b6920b6032e486592244c Mon Sep 17 00:00:00 2001 From: "Felipe M." Date: Mon, 9 Dec 2024 14:22:40 +0100 Subject: [PATCH 5/5] fix: make styles --- internal/view/assets/js/component/login.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/view/assets/js/component/login.js b/internal/view/assets/js/component/login.js index 7026bfaec..4e74ba25b 100644 --- a/internal/view/assets/js/component/login.js +++ b/internal/view/assets/js/component/login.js @@ -133,12 +133,15 @@ export default { const token = localStorage.getItem("shiori-token"); if (token) { try { - const response = await fetch(new URL("api/v1/auth/me", document.baseURI), { - headers: { - "Authorization": `Bearer ${token}` - } - }); - + const response = await fetch( + new URL("api/v1/auth/me", document.baseURI), + { + headers: { + Authorization: `Bearer ${token}`, + }, + }, + ); + if (response.ok) { // Valid session exists, emit login success this.$emit("login-success");