Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions internal/view/assets/js/component/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,42 @@ 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/me", 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;`;
document.cookie = `token=; Path=${
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");

// <input autofocus> 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) {
Expand Down
10 changes: 7 additions & 3 deletions internal/view/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<body>
<div id="app">
<login-view v-if="isLoggedIn === false" @login-success="onLoginSuccess"></login-view>
<login-view v-if="isLoggedIn === false && loginRequired" @login-success="onLoginSuccess"></login-view>
<div id="main-scene" v-else-if="isLoggedIn === true">
<div id="main-sidebar">
<a v-for="item in sidebarItems" :title="item.title" :class="{active: activePage === item.page}" @click="switchPage(item.page)">
Expand Down Expand Up @@ -59,7 +59,8 @@
'login-view': LoginComponent
},
data: {
isLoggedIn: false,
isLoggedIn: null,
loginRequired: false,
activePage: "page-home",
sidebarItems: [{
title: "Home",
Expand Down Expand Up @@ -103,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 => {
Expand Down Expand Up @@ -169,7 +171,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}`
}
Expand Down Expand Up @@ -197,6 +199,8 @@
if (isValid) {
this.loadSetting();
this.loadAccount();
} else {
this.loginRequired = true;
}
}
},
Expand Down