feat: 添加身份一致性检查,更新登录逻辑以清除不一致的缓存

This commit is contained in:
2026-06-25 18:39:08 +08:00
parent 95f5879c9a
commit 1d8b6936f2

View File

@@ -3,7 +3,7 @@ import { useNavigate } from "react-router";
import { motion } from "motion/react";
import { Heart } from "lucide-react";
import { useUser } from "../context/UserContext";
import { devLogin, fetchApi } from "../services/api";
import { devLogin, fetchApi, getToken, clearToken, decodeToken } from "../services/api";
import { debugLog, clearDebugLogs } from "../services/debug";
export function LoginPage() {
@@ -14,6 +14,21 @@ export function LoginPage() {
useEffect(() => {
clearDebugLogs();
// 身份一致性检查:URL 中的 openid 与缓存 token 不一致时清除缓存
const urlOpenid = new URLSearchParams(window.location.search).get('openid');
const cachedToken = getToken();
if (urlOpenid && cachedToken) {
try {
const payload = decodeToken(cachedToken);
if (payload.openid !== urlOpenid) {
debugLog(`[auth] openid 不一致: URL=${urlOpenid}, cache=${payload.openid},清除缓存`);
clearToken();
}
} catch (e) {
debugLog(`[auth] token 解码失败: ${e}`);
}
}
const params = new URLSearchParams(window.location.search);
debugLog(`URL: ${window.location.href.substring(0, 100)}`);
debugLog(`search: ${window.location.search}`);