fix(auth): scope login cookies to current host instead of shared primary domain

Writing Admin-Token/UserInfo to .sztzjy.com made every subdomain system
share and overwrite the same login state. Cookies are now host-only;
legacy shared-domain cookies are still cleaned up on set/remove so stale
duplicates cannot be read back.
dev-QQq
qinzhenpen 1 month ago
parent 47423a05d1
commit ef7375b026

@ -1,34 +1,33 @@
import Cookies from "js-cookie";
import { clearStudentDemoSession, getStudentDemoSession, isStudentDemo } from "@/utils/studentDemo";
const TokenKey = "Admin-Token";
const user = "UserInfo";
const SharedCookieDomain = ".sztzjy.com";
// 历史遗留:旧版本曾把 cookie 写到共享主域 .sztzjy.com,会污染其他子域系统的登录态。
// 现已改为 host-only(只写当前子域),此常量仅用于清理浏览器中遗留的旧主域 cookie。
const LegacySharedCookieDomain = ".sztzjy.com";
function isSharedCookieHost() {
if (typeof window === "undefined") return false;
const hostname = window.location.hostname.toLowerCase();
return hostname === "sztzjy.com" || hostname.endsWith(SharedCookieDomain);
return hostname === "sztzjy.com" || hostname.endsWith(LegacySharedCookieDomain);
}
function getAuthCookieOptions() {
if (!isSharedCookieHost()) return { path: "/" };
return {
domain: SharedCookieDomain,
path: "/",
secure: true,
sameSite: "Lax",
};
function removeLegacySharedCookie(name) {
if (isSharedCookieHost()) {
Cookies.remove(name, { domain: LegacySharedCookieDomain, path: "/" });
}
}
function setAuthCookie(name, value) {
if (isSharedCookieHost()) Cookies.remove(name, { path: "/" });
return Cookies.set(name, value, getAuthCookieOptions());
// 先清掉旧主域 cookie,避免同名双 cookie 导致读取到脏值
removeLegacySharedCookie(name);
return Cookies.set(name, value, { path: "/" });
}
function removeAuthCookie(name) {
Cookies.remove(name, { path: "/" });
if (isSharedCookieHost()) Cookies.remove(name, getAuthCookieOptions());
removeLegacySharedCookie(name);
}
export function getToken() {

Loading…
Cancel
Save