login gRPC

This commit is contained in:
filipriec
2025-03-31 16:16:07 +02:00
parent 7f5b671084
commit e2c326bf1e
3 changed files with 28 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ use crate::tui::terminal::core::TerminalCore;
use crate::state::pages::{form::FormState, auth::AuthState};
use crate::state::state::AppState;
use crate::services::grpc_client::GrpcClient;
use crate::services::auth::AuthClient;
use crate::tui::functions::common::{
form::{save as form_save, revert},
login::{save as login_save, cancel}
@@ -14,6 +15,7 @@ pub async fn handle_core_action(
form_state: &mut FormState,
auth_state: &mut AuthState,
grpc_client: &mut GrpcClient,
auth_client: &mut AuthClient,
terminal: &mut TerminalCore,
app_state: &mut AppState,
current_position: &mut u64,
@@ -22,7 +24,7 @@ pub async fn handle_core_action(
match action {
"save" => {
if app_state.ui.show_login {
let message = login_save(auth_state, grpc_client).await?;
let message = login_save(auth_state, auth_client).await?;
Ok((false, message))
} else {
let message = form_save(

View File

@@ -1,18 +1,35 @@
// src/tui/functions/common/login.rs
use crate::services::auth::AuthClient;
use crate::state::pages::auth::AuthState;
use crate::services::grpc_client::GrpcClient;
pub async fn save(
auth_state: &mut AuthState,
grpc_client: &mut GrpcClient,
auth_client: &mut AuthClient,
) -> Result<String, Box<dyn std::error::Error>> {
// Implement your login-specific save logic here
Ok("Login credentials saved - not implemented yet".to_string())
let identifier = auth_state.username.clone();
let password = auth_state.password.clone();
match auth_client.login(identifier, password).await {
Ok(response) => {
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;
Ok("Login successful!".to_string())
}
Err(e) => {
let error_message = format!("Login failed: {}", e);
auth_state.error_message = Some(error_message.clone());
Ok(error_message)
}
}
}
pub async fn cancel(
auth_state: &mut AuthState,
) -> String {
"Login credentials canceled - not implemented yet".to_string()
auth_state.username.clear();
auth_state.password.clear();
auth_state.error_message = None;
"Login canceled".to_string()
}

View File

@@ -2,6 +2,7 @@
use crate::tui::terminal::TerminalCore;
use crate::services::grpc_client::GrpcClient;
use crate::services::auth::AuthClient;
use crate::services::ui_service::UiService; // Add this import
use crate::tui::terminal::EventReader;
use crate::tui::functions::common::CommandHandler;
@@ -17,6 +18,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
let config = Config::load()?;
let mut terminal = TerminalCore::new()?;
let mut grpc_client = GrpcClient::new().await?;
let mut auth_client = AuthClient::new().await?;
let mut command_handler = CommandHandler::new();
let theme = Theme::from_str(&config.colors.theme);
let mut auth_state = AuthState::default();