// src/tui/functions/common/login.rs use crate::services::auth::AuthClient; use crate::state::pages::auth::AuthState; use crate::state::state::AppState; use crate::state::canvas_state::CanvasState; // Remove unused import if CanvasState is not directly used here // use crate::state::canvas_state::CanvasState; /// Attempts to log the user in using the provided credentials via gRPC. /// Updates AuthState and AppState on success or failure. pub async fn save( auth_state: &mut AuthState, auth_client: &mut AuthClient, app_state: &mut AppState, ) -> Result> { let identifier = auth_state.username.clone(); let password = auth_state.password.clone(); // Clear previous error/dialog state before attempting auth_state.error_message = None; // Use the helper to ensure dialog is hidden and cleared properly app_state.hide_dialog(); // Call the gRPC login method match auth_client.login(identifier, password).await { Ok(response) => { // Store authentication details on success auth_state.auth_token = Some(response.access_token.clone()); auth_state.user_id = Some(response.user_id.clone()); auth_state.role = Some(response.role.clone()); auth_state.set_has_unsaved_changes(false); let success_message = format!( "Login Successful!\n\n\ Access Token: {}\n\ Token Type: {}\n\ Expires In: {}\n\ User ID: {}\n\ Role: {}", response.access_token, response.token_type, response.expires_in, response.user_id, response.role ); // Use the helper method to configure and show the dialog // TODO Implement logic for pressing menu or exit buttons, not imeplementing it now, // need to do other more important stuff now" app_state.show_dialog( "Login Success", &success_message, vec!["Menu".to_string(), "Exit".to_string()], ); Ok("Login successful, details shown in dialog.".to_string()) } Err(e) => { let error_message = format!("{}", e); // Use the helper method to configure and show the dialog app_state.show_dialog( "Login Failed", &error_message, vec!["OK".to_string()], // Pass buttons here ); // REMOVE these lines: // app_state.ui.dialog.dialog_title = "Login Failed".to_string(); // app_state.ui.dialog.dialog_message = error_message.clone(); // app_state.ui.dialog.dialog_show = true; // app_state.ui.dialog.dialog_button_active = true; auth_state.set_has_unsaved_changes(true); Ok(format!("Login failed: {}", error_message)) } } } /// Reverts the login form fields to empty and returns to the previous screen (Intro). pub async fn revert( auth_state: &mut AuthState, app_state: &mut AppState, ) -> String { // Clear the input fields auth_state.username.clear(); auth_state.password.clear(); auth_state.error_message = None; auth_state.set_has_unsaved_changes(false); // Ensure dialog is hidden if revert is called // _app_state.hide_dialog(); // Uncomment if needed // Navigation logic (currently disabled in original code) // _app_state.ui.show_login = false; // _app_state.ui.show_intro = true; "Login reverted".to_string() }