refactor: simplify auth process in App using start_and_wait_for_auth
This commit is contained in:
+4
-28
@@ -29,7 +29,6 @@ pub struct App {
|
|||||||
pub api_client: Arc<ApiClient>,
|
pub api_client: Arc<ApiClient>,
|
||||||
pub needs_auth: bool,
|
pub needs_auth: bool,
|
||||||
pub auth_error: Option<String>,
|
pub auth_error: Option<String>,
|
||||||
pub auth_url: String,
|
|
||||||
auth_tx: std_mpsc::Sender<AuthEvent>,
|
auth_tx: std_mpsc::Sender<AuthEvent>,
|
||||||
auth_rx: std_mpsc::Receiver<AuthEvent>,
|
auth_rx: std_mpsc::Receiver<AuthEvent>,
|
||||||
sync_tx: mpsc::Sender<SyncCommand>,
|
sync_tx: mpsc::Sender<SyncCommand>,
|
||||||
@@ -49,7 +48,7 @@ pub enum SyncCommand {
|
|||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn new(db: Arc<Db>, api_client: Arc<ApiClient>, sync_tx: mpsc::Sender<SyncCommand>) -> Self {
|
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 (auth_tx, auth_rx) = std_mpsc::channel();
|
||||||
|
|
||||||
let show_popup = if has_token {
|
let show_popup = if has_token {
|
||||||
@@ -87,7 +86,6 @@ impl App {
|
|||||||
api_client,
|
api_client,
|
||||||
needs_auth: !has_token,
|
needs_auth: !has_token,
|
||||||
auth_error: None,
|
auth_error: None,
|
||||||
auth_url: String::new(),
|
|
||||||
auth_tx,
|
auth_tx,
|
||||||
auth_rx,
|
auth_rx,
|
||||||
sync_tx,
|
sync_tx,
|
||||||
@@ -99,38 +97,16 @@ impl App {
|
|||||||
let tx = self.auth_tx.clone();
|
let tx = self.auth_tx.clone();
|
||||||
|
|
||||||
self.auth_error = None;
|
self.auth_error = None;
|
||||||
self.auth_url.clear();
|
|
||||||
|
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
rt.block_on(async move {
|
rt.block_on(async move {
|
||||||
match api.start_auth_flow().await {
|
match api.start_and_wait_for_auth().await {
|
||||||
Ok((auth_url, _port)) => {
|
Ok(()) => {
|
||||||
// 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);
|
let _ = tx.send(AuthEvent::Ready);
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let msg = match &e {
|
let _ = tx.send(AuthEvent::Error(format!("{}", 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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user