fix: 修复登出按钮无效 — 同时清除磁盘 cookie 和浏览器内存 context
- BrowserManager 新增 clearContext() 方法,关闭并丢弃平台 context - deleteCookies 同时调用 cookieStore.delete + browser.clearContext - 前端登出后直接 resetStatus,不再触发浏览器请求检查状态
This commit is contained in:
@@ -193,6 +193,21 @@ export class BrowserManager {
|
|||||||
logger.debug({ platform }, 'Cookies saved');
|
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
|
* Wait for every in-flight platform queue to settle. Useful during
|
||||||
* graceful shutdown so that running operations finish before teardown.
|
* graceful shutdown so that running operations finish before teardown.
|
||||||
|
|||||||
@@ -140,15 +140,13 @@ export async function getLoginQRCode(
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete persisted cookies for the Xiaohongshu platform.
|
* Delete persisted cookies and clear the in-memory browser context so the
|
||||||
*
|
* next operation starts with a clean session.
|
||||||
* @param _browser - The shared BrowserManager instance (unused for now but
|
|
||||||
* passed for consistency; a future version may also clear
|
|
||||||
* the in-memory browser context).
|
|
||||||
*/
|
*/
|
||||||
export async function deleteCookies(_browser: BrowserManager): Promise<void> {
|
export async function deleteCookies(browser: BrowserManager): Promise<void> {
|
||||||
await cookieStore.delete(PLATFORM);
|
await cookieStore.delete(PLATFORM);
|
||||||
log.info('Xiaohongshu cookies deleted');
|
await browser.clearContext(PLATFORM);
|
||||||
|
log.info('Xiaohongshu cookies deleted and browser context cleared');
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -34,5 +34,11 @@ export function useLoginStatus(options: { auto?: boolean; intervalMs?: number }
|
|||||||
}
|
}
|
||||||
}, [refresh, auto, intervalMs]);
|
}, [refresh, auto, intervalMs]);
|
||||||
|
|
||||||
return { status, error, loading, refresh };
|
const reset = useCallback(() => {
|
||||||
|
setStatus(null);
|
||||||
|
setError(null);
|
||||||
|
setLoading(false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { status, error, loading, refresh, reset };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { useToast } from '@/context/ToastContext';
|
|||||||
import { getLoginQRCode, deleteCookies, checkLoginCookie } from '@/api/endpoints';
|
import { getLoginQRCode, deleteCookies, checkLoginCookie } from '@/api/endpoints';
|
||||||
|
|
||||||
export function LoginPage() {
|
export function LoginPage() {
|
||||||
const { status, loading: statusLoading, refresh: refreshStatus } = useLoginStatus();
|
const { status, loading: statusLoading, refresh: refreshStatus, reset: resetStatus } = useLoginStatus();
|
||||||
const { token } = useAuth();
|
const { token } = useAuth();
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ export function LoginPage() {
|
|||||||
const res = await deleteCookies();
|
const res = await deleteCookies();
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
toast('success', '已成功登出');
|
toast('success', '已成功登出');
|
||||||
void refreshStatus();
|
resetStatus();
|
||||||
} else {
|
} else {
|
||||||
toast('error', res.error?.message || '登出失败');
|
toast('error', res.error?.message || '登出失败');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user