fix: 修复登出按钮无效 — 同时清除磁盘 cookie 和浏览器内存 context

- BrowserManager 新增 clearContext() 方法,关闭并丢弃平台 context
- deleteCookies 同时调用 cookieStore.delete + browser.clearContext
- 前端登出后直接 resetStatus,不再触发浏览器请求检查状态
This commit is contained in:
2026-03-01 16:57:24 +08:00
parent 69a0f7b24c
commit 838b244929
4 changed files with 29 additions and 10 deletions
+15
View File
@@ -193,6 +193,21 @@ export class BrowserManager {
logger.debug({ platform }, 'Cookies saved');
}
/**
* Close and discard the browser context for a platform so the next
* operation creates a fresh one (without any cookies or storage).
*/
async clearContext(platform: string): Promise<void> {
const ctx = this.contexts.get(platform);
if (!ctx) return;
this.contexts.delete(platform);
await ctx.close().catch((err: unknown) => {
logger.warn({ err, platform }, 'Error closing context during clearContext');
});
logger.info({ platform }, 'Browser context cleared');
}
/**
* Wait for every in-flight platform queue to settle. Useful during
* graceful shutdown so that running operations finish before teardown.
+5 -7
View File
@@ -140,15 +140,13 @@ export async function getLoginQRCode(
// ---------------------------------------------------------------------------
/**
* Delete persisted cookies for the Xiaohongshu platform.
*
* @param _browser - The shared BrowserManager instance (unused for now but
* passed for consistency; a future version may also clear
* the in-memory browser context).
* Delete persisted cookies and clear the in-memory browser context so the
* next operation starts with a clean session.
*/
export async function deleteCookies(_browser: BrowserManager): Promise<void> {
export async function deleteCookies(browser: BrowserManager): Promise<void> {
await cookieStore.delete(PLATFORM);
log.info('Xiaohongshu cookies deleted');
await browser.clearContext(PLATFORM);
log.info('Xiaohongshu cookies deleted and browser context cleared');
}
// ---------------------------------------------------------------------------
+7 -1
View File
@@ -34,5 +34,11 @@ export function useLoginStatus(options: { auto?: boolean; intervalMs?: number }
}
}, [refresh, auto, intervalMs]);
return { status, error, loading, refresh };
const reset = useCallback(() => {
setStatus(null);
setError(null);
setLoading(false);
}, []);
return { status, error, loading, refresh, reset };
}
+2 -2
View File
@@ -10,7 +10,7 @@ import { useToast } from '@/context/ToastContext';
import { getLoginQRCode, deleteCookies, checkLoginCookie } from '@/api/endpoints';
export function LoginPage() {
const { status, loading: statusLoading, refresh: refreshStatus } = useLoginStatus();
const { status, loading: statusLoading, refresh: refreshStatus, reset: resetStatus } = useLoginStatus();
const { token } = useAuth();
const { toast } = useToast();
@@ -96,7 +96,7 @@ export function LoginPage() {
const res = await deleteCookies();
if (res.success) {
toast('success', '已成功登出');
void refreshStatus();
resetStatus();
} else {
toast('error', res.error?.message || '登出失败');
}