fix(setup): add logging and error details for setup debugging
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
This commit is contained in:
@@ -10,6 +10,7 @@ struct-patch = "0.5"
|
||||
rmp-serde = "1.3"
|
||||
bytes = "1"
|
||||
http = "1"
|
||||
tracing = "0.1"
|
||||
|
||||
# Leptos 0.8.7
|
||||
leptos = { version = "0.8.15", features = ["nightly", "msgpack"] }
|
||||
|
||||
@@ -28,8 +28,17 @@ impl Db {
|
||||
}
|
||||
|
||||
async fn run_migrations(&self) -> Result<()> {
|
||||
sqlx::migrate!("./migrations").run(&self.pool).await?;
|
||||
Ok(())
|
||||
tracing::info!("Starting database migrations...");
|
||||
match sqlx::migrate!("./migrations").run(&self.pool).await {
|
||||
Ok(_) => {
|
||||
tracing::info!("Database migrations completed successfully.");
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Database migration failed: {}", e);
|
||||
Err(e.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- User Operations ---
|
||||
|
||||
@@ -24,9 +24,19 @@ pub struct SetupStatus {
|
||||
pub async fn get_setup_status() -> Result<SetupStatus, ServerFnError> {
|
||||
use crate::DbContext;
|
||||
|
||||
let db_context = use_context::<DbContext>().ok_or_else(|| ServerFnError::new("DB Context missing"))?;
|
||||
tracing::info!("Checking setup status...");
|
||||
let db_context = use_context::<DbContext>().ok_or_else(|| {
|
||||
tracing::error!("DB Context missing in GetSetupStatus");
|
||||
ServerFnError::new("DB Context missing")
|
||||
})?;
|
||||
|
||||
let has_users = db_context.db.has_users().await
|
||||
.map_err(|e| ServerFnError::new(format!("DB error: {}", e)))?;
|
||||
.map_err(|e| {
|
||||
tracing::error!("DB error in GetSetupStatus: {}", e);
|
||||
ServerFnError::new(format!("DB error: {}", e))
|
||||
})?;
|
||||
|
||||
tracing::info!("Setup status: completed={}", has_users);
|
||||
|
||||
Ok(SetupStatus {
|
||||
completed: has_users,
|
||||
@@ -37,21 +47,33 @@ pub async fn get_setup_status() -> Result<SetupStatus, ServerFnError> {
|
||||
pub async fn setup(username: String, password: String) -> Result<(), ServerFnError> {
|
||||
use crate::DbContext;
|
||||
|
||||
let db_context = use_context::<DbContext>().ok_or_else(|| ServerFnError::new("DB Context missing"))?;
|
||||
tracing::info!("Attempting setup for user: {}", username);
|
||||
let db_context = use_context::<DbContext>().ok_or_else(|| {
|
||||
tracing::error!("DB Context missing in Setup");
|
||||
ServerFnError::new("DB Context missing")
|
||||
})?;
|
||||
|
||||
// Check if setup is already done
|
||||
let has_users = db_context.db.has_users().await.unwrap_or(false);
|
||||
if has_users {
|
||||
tracing::warn!("Setup attempt blocked: Setup already completed");
|
||||
return Err(ServerFnError::new("Setup already completed"));
|
||||
}
|
||||
|
||||
// Hash password (low cost for MIPS)
|
||||
let password_hash = bcrypt::hash(&password, 6)
|
||||
.map_err(|_| ServerFnError::new("Hashing error"))?;
|
||||
.map_err(|e| {
|
||||
tracing::error!("Hashing error: {}", e);
|
||||
ServerFnError::new("Hashing error")
|
||||
})?;
|
||||
|
||||
db_context.db.create_user(&username, &password_hash).await
|
||||
.map_err(|e| ServerFnError::new(format!("DB error: {}", e)))?;
|
||||
.map_err(|e| {
|
||||
tracing::error!("Failed to create user: {}", e);
|
||||
ServerFnError::new(format!("DB error: {}", e))
|
||||
})?;
|
||||
|
||||
tracing::info!("Setup completed successfully for user: {}", username);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user