feat: Implement user management with a new add_user binary and refactor state handling into a dedicated store module.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
use backend::items::Person;
|
||||
use backend::store::{self, User, save_state};
|
||||
use std::io::{self, Write};
|
||||
|
||||
fn main() {
|
||||
println!("Add User Tool");
|
||||
println!("-------------");
|
||||
|
||||
print!("Username: ");
|
||||
io::stdout().flush().unwrap();
|
||||
let mut username = String::new();
|
||||
io::stdin().read_line(&mut username).unwrap();
|
||||
let username = username.trim().to_string();
|
||||
|
||||
if username.is_empty() {
|
||||
println!("Username cannot be empty.");
|
||||
return;
|
||||
}
|
||||
|
||||
print!("Password: ");
|
||||
io::stdout().flush().unwrap();
|
||||
let mut password = String::new();
|
||||
io::stdin().read_line(&mut password).unwrap();
|
||||
let password = password.trim().to_string();
|
||||
|
||||
if password.is_empty() {
|
||||
println!("Password cannot be empty.");
|
||||
return;
|
||||
}
|
||||
|
||||
let (games, mut users) = store::load_state().unwrap_or_else(|| {
|
||||
println!("No existing state found. Creating new state.");
|
||||
(Vec::new(), Vec::new())
|
||||
});
|
||||
|
||||
if users.iter().any(|u| u.person.name == username) {
|
||||
println!("User '{}' already exists.", username);
|
||||
return;
|
||||
}
|
||||
|
||||
let password_hash = bcrypt::hash(&password, bcrypt::DEFAULT_COST).unwrap();
|
||||
|
||||
let new_user = User {
|
||||
person: Person {
|
||||
name: username.clone(),
|
||||
opinion: Vec::new(),
|
||||
},
|
||||
password_hash,
|
||||
};
|
||||
|
||||
users.push(new_user);
|
||||
save_state(&games, &users);
|
||||
|
||||
println!("User '{}' added successfully.", username);
|
||||
}
|
||||
Reference in New Issue
Block a user