修正
This commit is contained in:
@@ -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();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user