diff --git a/src/app.rs b/src/app.rs index 0dc6b07..73f7e13 100644 --- a/src/app.rs +++ b/src/app.rs @@ -29,7 +29,6 @@ pub struct App { pub api_client: Arc, pub needs_auth: bool, pub auth_error: Option, - pub auth_url: String, auth_tx: std_mpsc::Sender, auth_rx: std_mpsc::Receiver, sync_tx: mpsc::Sender, @@ -49,7 +48,7 @@ pub enum SyncCommand { impl App { pub fn new(db: Arc, api_client: Arc, sync_tx: mpsc::Sender) -> Self { - let has_token = api_client.token_file_exists(); + let has_token = api_client.has_token(); let (auth_tx, auth_rx) = std_mpsc::channel(); let show_popup = if has_token { @@ -87,7 +86,6 @@ impl App { api_client, needs_auth: !has_token, auth_error: None, - auth_url: String::new(), auth_tx, auth_rx, sync_tx, @@ -99,38 +97,16 @@ impl App { let tx = self.auth_tx.clone(); self.auth_error = None; - self.auth_url.clear(); std::thread::spawn(move || { let rt = tokio::runtime::Runtime::new().unwrap(); rt.block_on(async move { - match api.start_auth_flow().await { - Ok((auth_url, _port)) => { - // Try to open browser, but it's OK if it fails - ApiClient::open_browser(&auth_url); - - // Poll until token is ready - loop { - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; - if api.token_is_ready().await { - let _ = tx.send(AuthEvent::Ready); - break; - } - } + match api.start_and_wait_for_auth().await { + Ok(()) => { + let _ = tx.send(AuthEvent::Ready); } Err(e) => { - let msg = match &e { - crate::infrastructure::api::ApiError::Network(s) => { - format!("Network: {}", s) - } - crate::infrastructure::api::ApiError::Auth(s) => { - format!("Auth: {}", s) - } - crate::infrastructure::api::ApiError::Api(s) => { - format!("API: {}", s) - } - }; - let _ = tx.send(AuthEvent::Error(msg)); + let _ = tx.send(AuthEvent::Error(format!("{}", e))); } } });