refactor: simplify auth process in App using start_and_wait_for_auth
This commit is contained in:
+5
-29
@@ -29,7 +29,6 @@ pub struct App {
|
||||
pub api_client: Arc<ApiClient>,
|
||||
pub needs_auth: bool,
|
||||
pub auth_error: Option<String>,
|
||||
pub auth_url: String,
|
||||
auth_tx: std_mpsc::Sender<AuthEvent>,
|
||||
auth_rx: std_mpsc::Receiver<AuthEvent>,
|
||||
sync_tx: mpsc::Sender<SyncCommand>,
|
||||
@@ -49,7 +48,7 @@ pub enum SyncCommand {
|
||||
|
||||
impl App {
|
||||
pub fn new(db: Arc<Db>, api_client: Arc<ApiClient>, sync_tx: mpsc::Sender<SyncCommand>) -> 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)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user