63 lines
2.4 KiB
Rust
63 lines
2.4 KiB
Rust
// 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;
|
|
|
|
/// 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<String, Box<dyn std::error::Error>> {
|
|
let identifier = auth_state.username.clone();
|
|
let password = auth_state.password.clone();
|
|
|
|
// 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);
|
|
auth_state.user_id = Some(response.user_id);
|
|
auth_state.role = Some(response.role);
|
|
auth_state.error_message = None;
|
|
auth_state.set_has_unsaved_changes(false); // Mark as "saved"
|
|
|
|
// Update app state to transition from login to the main form view
|
|
app_state.ui.show_login = false;
|
|
app_state.ui.show_form = true; // Assuming form is the next view
|
|
|
|
Ok("Login successful!".to_string())
|
|
}
|
|
Err(e) => {
|
|
// Store error message on failure
|
|
let error_message = format!("Login failed: {}", e);
|
|
auth_state.error_message = Some(error_message.clone());
|
|
// Keep unsaved changes true if login fails, allowing retry/revert
|
|
auth_state.set_has_unsaved_changes(true);
|
|
Ok(error_message) // Return error message to display
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Reverts the login form fields to empty and returns to the previous screen (Intro).
|
|
/// This function is now named 'revert' to match the 'form' counterpart.
|
|
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; // Clear any previous error
|
|
auth_state.set_has_unsaved_changes(false); // Fields are cleared, no unsaved changes
|
|
|
|
// Update app state to hide login and show the previous screen (Intro)
|
|
app_state.ui.show_login = false;
|
|
app_state.ui.show_intro = true; // Assuming Intro is the screen before login
|
|
|
|
"Login reverted".to_string()
|
|
}
|
|
|