From 2136c3aa25db79ad739ab0261caafa00533df765 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Fri, 1 Sep 2023 22:21:16 +0800 Subject: [PATCH] refactor: show retrying UI --- gpgui/src-tauri/src/auth.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/gpgui/src-tauri/src/auth.rs b/gpgui/src-tauri/src/auth.rs index 2cd4846..0ebe9d7 100644 --- a/gpgui/src-tauri/src/auth.rs +++ b/gpgui/src-tauri/src/auth.rs @@ -84,9 +84,18 @@ impl AuthData { } fn check(&self) -> bool { - let username_valid = self.username.clone().is_some_and(|username| !username.is_empty()); - let prelogin_cookie_valid = self.prelogin_cookie.clone().is_some_and(|val| val.len() > 5); - let portal_userauthcookie_valid = self.portal_userauthcookie.clone().is_some_and(|val| val.len() > 5); + let username_valid = self + .username + .clone() + .is_some_and(|username| !username.is_empty()); + let prelogin_cookie_valid = self + .prelogin_cookie + .clone() + .is_some_and(|val| val.len() > 5); + let portal_userauthcookie_valid = self + .portal_userauthcookie + .clone() + .is_some_and(|val| val.len() > 5); username_valid && (prelogin_cookie_valid || portal_userauthcookie_valid) } @@ -247,7 +256,7 @@ async fn monitor_events(window: &Window, event_rx: mpsc::Receiver) -> } async fn monitor_auth_event(window: &Window, mut event_rx: mpsc::Receiver) -> AuthData { - info!("Monitoring auth events"); + info!("Start monitoring auth events"); let (cancel_timeout_tx, cancel_timeout_rx) = mpsc::channel::<()>(1); let cancel_timeout_rx = Arc::new(Mutex::new(cancel_timeout_rx)); @@ -272,6 +281,23 @@ async fn monitor_auth_event(window: &Window, mut event_rx: mpsc::Receiver { + let _ = window.with_webview(|wv| { + let wv = wv.inner(); + let url = wv.uri().unwrap_or("".into()); + + info!("Injecting loading element to {}", redact_url(&url)); + wv.run_javascript( + r#" + var loading = document.createElement("div"); + loading.innerHTML = '
Got invalid token, retrying...
'; + loading.style = "position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(255, 255, 255, 0.85); z-index: 99999;"; + document.body.appendChild(loading); + "#, + Cancellable::NONE, + |_| info!("Injected loading element successfully"), + ); + }); + // Found the invalid token, means that user is authenticated, keep retrying and no need to show the window warn!( "Attempt #{} failed, found invalid token, retrying",