1.修复问题同时打开多个标签页,退出登录,其它几个标签页并没有退出登录
2.点击智能体时,若当前已经打开智能体标签,则不会再打开新标签页
This commit is contained in:
1 parent
3642ed8529
commit
63ea9d7ed7
6 files changed
+201
-19
No files matched your search
@@ -58,13 +58,86 @@ export const saveAuthUser = (user: AuthUser, remember = false): void => {
|
||||
}
|
||||
};
|
||||
|
||||
export const clearAuthUser = (): void => {
|
||||
const AUTH_LOGOUT_KEY = 'hxops_auth_logout';
|
||||
const AUTH_BC_NAME = 'hxops_auth';
|
||||
|
||||
let _authBroadcast: BroadcastChannel | null = null;
|
||||
let _authSyncInitialized = false;
|
||||
|
||||
const clearAuthStorage = (): void => {
|
||||
localStorage.removeItem('hxops_user');
|
||||
sessionStorage.removeItem('hxops_user');
|
||||
sessionStorage.removeItem('hxops_wecom_state');
|
||||
sessionStorage.removeItem('hxops_casdoor_state');
|
||||
};
|
||||
|
||||
const broadcastLogout = (): void => {
|
||||
try {
|
||||
localStorage.setItem(AUTH_LOGOUT_KEY, String(Date.now()));
|
||||
localStorage.removeItem(AUTH_LOGOUT_KEY);
|
||||
} catch {
|
||||
// ignore quota / private mode errors
|
||||
}
|
||||
try {
|
||||
if (!_authBroadcast) {
|
||||
_authBroadcast = new BroadcastChannel(AUTH_BC_NAME);
|
||||
}
|
||||
_authBroadcast.postMessage({ type: 'logout', ts: Date.now() });
|
||||
} catch {
|
||||
// BroadcastChannel may be unavailable
|
||||
}
|
||||
};
|
||||
|
||||
const dispatchLogout = (detail: Record<string, unknown> = {}): void => {
|
||||
window.dispatchEvent(new CustomEvent('hxops:logout', { detail }));
|
||||
};
|
||||
|
||||
const handleRemoteLogout = (): void => {
|
||||
if (!getStoredAuthUser()) return;
|
||||
clearAuthStorage();
|
||||
dispatchLogout({ remote: true });
|
||||
};
|
||||
|
||||
/** Clear credentials and notify other tabs in the same browser. */
|
||||
export const logoutAuth = (): void => {
|
||||
clearAuthStorage();
|
||||
broadcastLogout();
|
||||
dispatchLogout({ source: 'local' });
|
||||
};
|
||||
|
||||
export const clearAuthUser = (): void => {
|
||||
logoutAuth();
|
||||
};
|
||||
|
||||
/** Listen for logout in other tabs (storage + BroadcastChannel). */
|
||||
export const initAuthSync = (): void => {
|
||||
if (_authSyncInitialized) return;
|
||||
_authSyncInitialized = true;
|
||||
|
||||
window.addEventListener('storage', (event: StorageEvent) => {
|
||||
if (event.key === 'hxops_user' && event.newValue === null && event.oldValue != null) {
|
||||
handleRemoteLogout();
|
||||
return;
|
||||
}
|
||||
if (event.key === AUTH_LOGOUT_KEY && event.newValue != null) {
|
||||
handleRemoteLogout();
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
if (!_authBroadcast) {
|
||||
_authBroadcast = new BroadcastChannel(AUTH_BC_NAME);
|
||||
}
|
||||
_authBroadcast.onmessage = (event: MessageEvent) => {
|
||||
if (event.data?.type === 'logout') {
|
||||
handleRemoteLogout();
|
||||
}
|
||||
};
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
};
|
||||
|
||||
let _refreshing: Promise<string | null> | null = null;
|
||||
|
||||
export const refreshAccessToken = async (): Promise<string | null> => {
|
||||
|
||||
Reference in new issue
Block a user