This commit is contained in:
admin
2026-05-22 23:05:25 +09:00
parent cea3016fbe
commit e8690d130b
23 changed files with 581 additions and 187 deletions

View File

@@ -1,41 +1,59 @@
/**
* 认证和会话管理
* 認証・セッション管理
*/
// 检查用户是否已登录
// ──────────────────────────────────────
// ページ描画前に即座に認証確認(フラッシュ防止)
// ──────────────────────────────────────
(function () {
if (!window.location.pathname.includes("login.html")) {
if (!localStorage.getItem("currentUser")) {
// ページが見えてしまわないよう即座に非表示にしてからリダイレクト
document.documentElement.style.visibility = "hidden";
window.location.replace("/login.html");
}
}
})();
// bfcache戻る/進むボタン)対応
// ブラウザがキャッシュからページを復元した場合も認証を再確認する
window.addEventListener("pageshow", function (event) {
if (!window.location.pathname.includes("login.html")) {
if (!localStorage.getItem("currentUser")) {
// bfcacheから復元されたページを即座に非表示にしてリダイレクト
document.documentElement.style.visibility = "hidden";
window.location.replace("/login.html");
}
}
});
function checkAuth() {
const user = localStorage.getItem("currentUser");
if (!user) {
// 用户未登录,重定向到登录页面
window.location.href = "/login.html";
window.location.replace("/login.html");
return false;
}
return true;
}
// 获取当前登录的用户信息
function getCurrentUser() {
const user = localStorage.getItem("currentUser");
return user ? JSON.parse(user) : null;
}
// 保存用户信息(登录时使用)
function setCurrentUser(userData) {
localStorage.setItem("currentUser", JSON.stringify(userData));
}
// 注销用户
function logout() {
localStorage.removeItem("currentUser");
window.location.href = "/login.html";
sessionStorage.clear();
// replace() を使うことでログイン前の履歴に戻れなくなる
window.location.replace("/login.html");
}
// 在页面加载时检查认证(仅在非登录页面使用)
document.addEventListener("DOMContentLoaded", function () {
const currentPage = window.location.pathname;
// 排除登录页面的检查
if (!currentPage.includes("login.html")) {
if (!window.location.pathname.includes("login.html")) {
checkAuth();
}
});