login with dialog menu

This commit is contained in:
filipriec
2025-04-05 17:52:37 +02:00
parent 0219dc0ede
commit 1baf89dde6

View File

@@ -14,29 +14,61 @@ pub async fn save(
let identifier = auth_state.username.clone();
let password = auth_state.password.clone();
// Clear previous error/dialog state before attempting
auth_state.error_message = None;
app_state.ui.dialog.show_dialog = false;
// 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.auth_token = Some(response.access_token.clone()); // Clone response fields
auth_state.user_id = Some(response.user_id.clone());
auth_state.role = Some(response.role.clone());
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
// Format the successful response for the dialog
// Note: Long tokens might make the dialog wide. Consider truncating if needed.
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, // Ensure this type implements Display or use .to_string()
response.user_id,
response.role
);
Ok("Login successful!".to_string())
// Configure and show the success dialog
app_state.ui.dialog.dialog_title = "Login Success".to_string();
app_state.ui.dialog.dialog_message = success_message;
app_state.ui.dialog.show_dialog = true;
app_state.ui.dialog.dialog_button_active = true; // Make OK button active
// Return a simple success indicator; the dialog shows details
Ok("Login successful, details shown in dialog.".to_string())
}
Err(e) => {
// Store error message on failure
let error_message = format!("Login failed: {}", e);
auth_state.error_message = Some(error_message.clone());
// Format the error message for the dialog
let error_message = format!("{}", e);
// Configure and show the error dialog
app_state.ui.dialog.dialog_title = "Login Failed".to_string();
app_state.ui.dialog.dialog_message = error_message.clone(); // Clone for return
app_state.ui.dialog.show_dialog = true;
app_state.ui.dialog.dialog_button_active = true; // Make OK button active
// 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
// Return the error message; the dialog also shows it
// Using Ok here because the 'save' operation itself didn't panic,
// even though the underlying login failed.
Ok(format!("Login failed: {}", error_message))
}
}
}