feat: 添加身份一致性检查,更新登录逻辑以清除不一致的缓存
This commit is contained in:
@@ -3,7 +3,7 @@ import { useNavigate } from "react-router";
|
|||||||
import { motion } from "motion/react";
|
import { motion } from "motion/react";
|
||||||
import { Heart } from "lucide-react";
|
import { Heart } from "lucide-react";
|
||||||
import { useUser } from "../context/UserContext";
|
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";
|
import { debugLog, clearDebugLogs } from "../services/debug";
|
||||||
|
|
||||||
export function LoginPage() {
|
export function LoginPage() {
|
||||||
@@ -14,6 +14,21 @@ export function LoginPage() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
clearDebugLogs();
|
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);
|
const params = new URLSearchParams(window.location.search);
|
||||||
debugLog(`URL: ${window.location.href.substring(0, 100)}`);
|
debugLog(`URL: ${window.location.href.substring(0, 100)}`);
|
||||||
debugLog(`search: ${window.location.search}`);
|
debugLog(`search: ${window.location.search}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user