ws auth
This commit is contained in:
@@ -586,6 +586,7 @@ where
|
|||||||
error!("Failed to save auth pass: {}", e);
|
error!("Failed to save auth pass: {}", e);
|
||||||
} else {
|
} else {
|
||||||
info!("Authentication successful!");
|
info!("Authentication successful!");
|
||||||
|
crate::core::services::ws::init_ws_client().await;
|
||||||
on_success();
|
on_success();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
use rust_socketio::{ClientBuilder, Payload, RawClient};
|
use rust_socketio::{ClientBuilder, Payload, RawClient};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use tauri::async_runtime;
|
use tauri::async_runtime;
|
||||||
use tracing::error;
|
use tracing::{error, info};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{models::app_config::AppConfig, state::FDOLL},
|
core::{models::app_config::AppConfig, state::FDOLL},
|
||||||
lock_r,
|
lock_r, lock_w,
|
||||||
services::cursor::CursorPosition,
|
services::cursor::CursorPosition,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -45,19 +45,54 @@ pub async fn report_cursor_data(cursor_position: CursorPosition) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_ws_client(app_config: &AppConfig) -> rust_socketio::client::Client {
|
pub async fn init_ws_client() {
|
||||||
let client = match ClientBuilder::new(
|
let app_config = {
|
||||||
app_config
|
let guard = lock_r!(FDOLL);
|
||||||
.api_base_url
|
guard.app_config.clone()
|
||||||
.as_ref()
|
|
||||||
.expect("Missing API base URL"),
|
|
||||||
)
|
|
||||||
.namespace("/")
|
|
||||||
.on("pong", on_pong)
|
|
||||||
.connect()
|
|
||||||
{
|
|
||||||
Ok(c) => c,
|
|
||||||
Err(_) => todo!("TODO error handling"),
|
|
||||||
};
|
};
|
||||||
client
|
|
||||||
|
let ws_client = build_ws_client(&app_config).await;
|
||||||
|
|
||||||
|
let mut guard = lock_w!(FDOLL);
|
||||||
|
if let Some(clients) = guard.clients.as_mut() {
|
||||||
|
clients.ws_client = Some(ws_client);
|
||||||
|
}
|
||||||
|
info!("WebSocket client initialized after authentication");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn build_ws_client(app_config: &AppConfig) -> rust_socketio::client::Client {
|
||||||
|
let token = crate::core::services::auth::get_access_token()
|
||||||
|
.await
|
||||||
|
.expect("No access token available for WebSocket connection");
|
||||||
|
|
||||||
|
info!("Building WebSocket client with authentication");
|
||||||
|
|
||||||
|
let api_base_url = app_config
|
||||||
|
.api_base_url
|
||||||
|
.clone()
|
||||||
|
.expect("Missing API base URL");
|
||||||
|
|
||||||
|
let client = async_runtime::spawn_blocking(move || {
|
||||||
|
ClientBuilder::new(api_base_url)
|
||||||
|
.namespace("/")
|
||||||
|
.on("pong", on_pong)
|
||||||
|
.auth(json!({ "token": token }))
|
||||||
|
.connect()
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.expect("Failed to spawn blocking task");
|
||||||
|
|
||||||
|
match client {
|
||||||
|
Ok(c) => {
|
||||||
|
info!("WebSocket client connected successfully");
|
||||||
|
c
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to connect WebSocket: {:?}", e);
|
||||||
|
panic!(
|
||||||
|
"TODO: better error handling for WebSocket connection - {}",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
core::{
|
core::{
|
||||||
models::app_config::{AppConfig, AuthConfig},
|
models::app_config::{AppConfig, AuthConfig},
|
||||||
services::{
|
services::auth::{load_auth_pass, AuthPass},
|
||||||
auth::{load_auth_pass, AuthPass},
|
|
||||||
ws::build_ws_client,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
lock_w,
|
lock_w,
|
||||||
};
|
};
|
||||||
@@ -78,24 +75,15 @@ pub fn init_fdoll_state() {
|
|||||||
});
|
});
|
||||||
info!("Initialized HTTP client");
|
info!("Initialized HTTP client");
|
||||||
|
|
||||||
// Clone app_config for async task
|
let has_auth = guard.auth_pass.is_some();
|
||||||
let app_config = guard.app_config.clone();
|
|
||||||
|
|
||||||
// Drop the write lock before spawning async task
|
|
||||||
drop(guard);
|
drop(guard);
|
||||||
|
|
||||||
// Initialize WebSocket client in a blocking task to avoid runtime conflicts
|
if has_auth {
|
||||||
async_runtime::spawn(async move {
|
async_runtime::spawn(async move {
|
||||||
let ws_client = async_runtime::spawn_blocking(move || build_ws_client(&app_config))
|
crate::core::services::ws::init_ws_client().await;
|
||||||
.await
|
});
|
||||||
.expect("Failed to initialize WebSocket client");
|
}
|
||||||
|
|
||||||
let mut guard = lock_w!(FDOLL);
|
|
||||||
if let Some(clients) = guard.clients.as_mut() {
|
|
||||||
clients.ws_client = Some(ws_client);
|
|
||||||
}
|
|
||||||
info!("Initialized FDOLL state with WebSocket client");
|
|
||||||
});
|
|
||||||
|
|
||||||
info!("Initialized FDOLL state (WebSocket client initializing asynchronously)");
|
info!("Initialized FDOLL state (WebSocket client initializing asynchronously)");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user