login refactored

This commit is contained in:
filipriec
2025-04-18 23:26:48 +02:00
parent dc6c1ce43c
commit d18f7862ab
3 changed files with 93 additions and 92 deletions

View File

@@ -230,50 +230,8 @@ impl EventHandler {
}
UiContext::Login => {
let login_action_message = match index {
0 => { // "Login" button pressed
let username = login_state.username.clone();
let password = login_state.password.clone();
// 1. Client-side validation
if username.trim().is_empty() {
app_state.show_dialog(
"Login Failed",
"Username/Email cannot be empty.",
vec!["OK".to_string()],
DialogPurpose::LoginFailed,
);
// Return a message, no need to modify login_state here
// as it will be cleared when result is processed
"Username cannot be empty.".to_string()
} else {
// 2. Show Loading Dialog
app_state.show_loading_dialog("Logging In", "Please wait...");
// 3. Clone sender for the task (needs sender from ui.rs)
// NOTE: We need access to login_result_sender here.
// This requires passing it into EventHandler or handle_event.
// Let's assume it's added to EventHandler state for now.
let sender = self.login_result_sender.clone(); // Assumes sender is part of EventHandler state
// 4. Spawn the login task
spawn(async move {
let login_outcome = match AuthClient::new().await {
Ok(mut auth_client) => {
match auth_client.login(username.clone(), password).await
.with_context(|| format!("Spawned login task failed for identifier: {}", username))
{
Ok(response) => login::LoginResult::Success(response),
Err(e) => login::LoginResult::Failure(format!("{}", e)),
}
}
Err(e) => login::LoginResult::ConnectionError(format!("Failed to create AuthClient: {}", e)),
};
let _ = sender.send(login_outcome).await; // Handle error?
});
// 5. Return immediately
"Login initiated.".to_string()
}
0 => {
login::initiate_login(login_state, app_state, self.login_result_sender.clone())
},
1 => login::back_to_main(login_state, app_state, buffer_state).await,
_ => "Invalid Login Option".to_string(),