feat: refactor and refine authentication system with decoupled user management and admin console

This commit is contained in:
2026-05-30 14:05:11 +05:00
parent f35908095c
commit f6ea8a99d9
17 changed files with 816 additions and 191 deletions
+36
View File
@@ -75,6 +75,42 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let task_repo = MongoTaskRepository::new(db.clone());
let dev_repo = MongoDeveloperRepository::new(db.clone());
// Auto-provision initial administrator if users collection is empty
let users_count = db.collection::<crate::auth::models::User>("users")
.count_documents(mongodb::bson::doc! {})
.await?;
if users_count == 0 {
use rand::{distributions::Alphanumeric, Rng};
let random_password: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(16)
.map(char::from)
.collect();
let password_hash = bcrypt::hash(&random_password, bcrypt::DEFAULT_COST)?;
let admin_username = "admin";
let admin_user = crate::auth::models::User {
id: None,
username: admin_username.to_string(),
password_hash,
is_admin: true,
created_at: chrono::Utc::now(),
};
db.collection::<crate::auth::models::User>("users")
.insert_one(admin_user)
.await?;
info!("\n\n\
======================================================\n\
CREATED INITIAL ADMINISTRATOR ACCOUNT:\n\
Username: {}\n\
Password: {}\n\
======================================================\n\n",
admin_username, random_password);
}
// 5. Initialize shared AppState
let state = AppState {
config: config.clone(),