use rpassword::read_password;
use std::io::{Write, stdin, stdout};
pub struct User {
pub username: String,
pub password: String,
}
impl User {
pub fn new() -> Self {
Self {
username: String::new(),
password: String::new(),
}
}
pub fn from(username: &str, password: &str) -> Self {
Self {
username: String::from(username),
password: String::from(password),
}
}
pub fn register() -> Self {
let mut new_username = String::new();
let mut new_password = String::new();
print!("New username: ");
stdout().flush().unwrap();
stdin()
.read_line(&mut new_username)
.expect("Invalid username input!");
let new_username = new_username.trim();
print!("New password (all spaces are removed): ");
stdout().flush().unwrap();
new_password = read_password()
.expect("Invalid password input!")
.replace(" ", "");
let new_password = new_password.trim();
Self::from(new_username, new_password)
}
pub fn verify(&self) -> bool {
let mut password = String::new();
print!("Password: ");
stdout().flush().unwrap();
password = read_password()
.expect("Invalid password input!");
let password = password.trim();
if password == self.password{
true
} else {
false
}
}
}