fix: stabilize auth sync and stream recovery for ji

This commit is contained in:
吉鑫升 committed 2026-08-17 19:27:39 +08:00
1 parent 5bf08c83a6
commit 68c4896088
6 files changed
+118 -50

No files matched your search

+58
View File
@@ -45,6 +45,64 @@ export function getStoredAuth(): LoginByUsernameResponse | null {
}
}
function normalizeIdentityPart(value: unknown): string {
return typeof value === "string" ? value.trim().toLowerCase() : "";
}
function readUserInfoIdentity(): string | null {
let info: Record<string, unknown> | null = null;
try {
const raw = authStorageGet("userInfo");
if (raw) {
const parsed = JSON.parse(raw);
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
info = parsed as Record<string, unknown>;
}
}
} catch {
info = null;
}
if (!info) return null;
const candidates = [
["yUserId", info.yUserId],
["userName", info.userName],
["userId", info.userId],
] as const;
for (const [prefix, value] of candidates) {
const normalized = normalizeIdentityPart(value);
if (normalized) return `${prefix}:${normalized}`;
}
return null;
}
/**
* A stable cross-tab identity key for "did the account really change?" checks.
*
* Some external login paths can re-issue a backend ``user_id`` while resolving
* to the same business account. Prefer business-facing identifiers
* (email/username/userInfo) and keep ``user_id`` only as the final fallback.
*/
export function getAccountIdentityKey(
auth: LoginByUsernameResponse | null = getStoredAuth(),
): string | null {
if (!auth) return null;
const email = normalizeIdentityPart(auth.email);
if (email) return `email:${email}`;
const username = normalizeIdentityPart(auth.username);
if (username) return `username:${username}`;
const userInfoIdentity = readUserInfoIdentity();
if (userInfoIdentity) return `userInfo:${userInfoIdentity}`;
const userId = normalizeIdentityPart(auth.user_id);
return userId ? `user_id:${userId}` : null;
}
export function setStoredAuth(auth: LoginByUsernameResponse) {
if (!isBrowser()) {
return;