feat: implement dfguide-registrar project with Postgres user registration functionality
This commit is contained in:
+52
@@ -0,0 +1,52 @@
|
||||
mod config;
|
||||
mod db;
|
||||
mod validation;
|
||||
|
||||
use anyhow::Result;
|
||||
use dialoguer::{Input, Password, theme::ColorfulTheme};
|
||||
use dotenvy::dotenv;
|
||||
|
||||
use crate::db::RegistrationInput;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
dotenv().ok();
|
||||
|
||||
let config = config::AppConfig::from_env()?;
|
||||
let pool = db::connect(&config).await?;
|
||||
|
||||
println!("Delta Force Guide 用户注册");
|
||||
println!("请依次输入用户名、邮箱和密码。\n");
|
||||
|
||||
let registration = prompt_registration()?;
|
||||
let user_id = db::register_local_user(&pool, registration).await?;
|
||||
|
||||
println!("\n注册成功,user_id = {user_id}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prompt_registration() -> Result<RegistrationInput> {
|
||||
let theme = ColorfulTheme::default();
|
||||
|
||||
let username = Input::<String>::with_theme(&theme)
|
||||
.with_prompt("用户名")
|
||||
.validate_with(|input: &String| validation::validate_username(input).map_err(str::to_owned))
|
||||
.interact_text()?;
|
||||
|
||||
let email = Input::<String>::with_theme(&theme)
|
||||
.with_prompt("电子邮箱")
|
||||
.validate_with(|input: &String| validation::validate_email(input).map_err(str::to_owned))
|
||||
.interact_text()?;
|
||||
|
||||
let password = Password::with_theme(&theme)
|
||||
.with_prompt("密码")
|
||||
.with_confirmation("再次输入密码", "两次输入的密码不一致")
|
||||
.validate_with(|input: &String| validation::validate_password(input).map_err(str::to_owned))
|
||||
.interact()?;
|
||||
|
||||
Ok(RegistrationInput {
|
||||
username: validation::normalize_username(&username)?,
|
||||
email: validation::normalize_email(&email)?,
|
||||
password,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user