From 188783f999aa5f7a892ce4ed7ee2000f5c7e98f2 Mon Sep 17 00:00:00 2001 From: zhaojisen <1301338853@qq.com> Date: Wed, 14 Jan 2026 13:40:51 +0800 Subject: [PATCH] fix: Fix the issue where the channel is closed during login --- src-tauri/src/commands/auth_login.rs | 18 +++++++++++++++--- src-tauri/src/lib.rs | 3 ++- ui/components/SideBar/profile.vue | 8 +++++++- ui/pages/auth/browser.vue | 13 ++++++++++--- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src-tauri/src/commands/auth_login.rs b/src-tauri/src/commands/auth_login.rs index 2b61850..672a1ef 100644 --- a/src-tauri/src/commands/auth_login.rs +++ b/src-tauri/src/commands/auth_login.rs @@ -201,9 +201,13 @@ pub async fn auth_login( .expect("Client no build"); // 等待 deep link 回调传回 code/state - let callback = rx - .await - .map_err(|_| anyhow::anyhow!("auth flow cancelled or timed out"))?; + let callback = match rx.await { + Ok(callback) => callback, + Err(_) => { + log::info!("auth flow cancelled"); + return Ok(()); + } + }; // 防止 CSRF if let Some(state) = callback.state.as_ref() { @@ -282,6 +286,14 @@ pub async fn auth_login( fut.await.map_err(|e| e.to_string()) } +#[tauri::command] +pub fn auth_cancel(flow_state: State<'_, AuthFlowState>) -> Result<(), String> { + if let Ok(mut guard) = flow_state.pending.lock() { + let _ = guard.take(); + } + Ok(()) +} + /// 确保 access_token 新鲜;如过期则用 refresh_token 刷新并更新存储 pub async fn ensure_fresh_token( _app: &AppHandle, diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 74cd48a..0c30fc9 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -7,7 +7,7 @@ use crate::setup::apply_window_effects; use crate::setup::menu::{build_menu, handle_menu_event}; use crate::setup::setup_tray; -use crate::commands::auth_login::{auth_login, handle_auth_callback, AuthFlowState}; +use crate::commands::auth_login::{auth_cancel, auth_login, handle_auth_callback, AuthFlowState}; use crate::commands::get_asset_detail::get_asset_detail; use crate::commands::get_assets::get_assets; use crate::commands::get_config::get_config; @@ -153,6 +153,7 @@ pub fn run() { pull_up, unfavorite, auth_login, + auth_cancel, get_assets, get_config, get_setting, diff --git a/ui/components/SideBar/profile.vue b/ui/components/SideBar/profile.vue index 6864de4..b236be1 100644 --- a/ui/components/SideBar/profile.vue +++ b/ui/components/SideBar/profile.vue @@ -622,7 +622,13 @@ onBeforeUnmount(() => { - + {{ t("Common.Login") }} diff --git a/ui/pages/auth/browser.vue b/ui/pages/auth/browser.vue index 359361e..a6c2c5c 100644 --- a/ui/pages/auth/browser.vue +++ b/ui/pages/auth/browser.vue @@ -29,6 +29,10 @@ const headingClass = computed(() => (userTheme.value === "dark" ? "text-gray-50" const subTextClass = computed(() => (userTheme.value === "dark" ? "text-gray-300" : "text-gray-600")); const back = () => { + void useTauriCoreInvoke("auth_cancel", {}).catch((error) => { + console.debug("auth_cancel failed:", error); + }); + navigateTo({ path: localePath({ path: "/" }) }); @@ -76,8 +80,8 @@ onMounted(async () => { }); unlistenLoginSuccessRef.value = await useTauriEventListen("login-success-detected", async (event) => { - const { status, profile, bearer, current_org, resolved_site, permission_orgs, xpack_license_valid } - = event.payload as UserIntiInfo & { bearer: string }; + const { status, profile, bearer, current_org, resolved_site, permission_orgs, xpack_license_valid } = + event.payload as UserIntiInfo & { bearer: string }; const profileData = JSON.parse((profile as any).data); const currentOrgData = JSON.parse((current_org as any).data); @@ -123,6 +127,9 @@ onMounted(async () => { onBeforeUnmount(() => { if (unlistenAuth.value) unlistenAuth.value(); if (unlistenLoginSuccessRef.value) unlistenLoginSuccessRef.value(); + void useTauriCoreInvoke("auth_cancel", {}).catch((error) => { + console.debug("auth_cancel failed:", error); + }); }); @@ -130,7 +137,7 @@ onBeforeUnmount(() => {
- logo + logo