axumでユーザの登録画面、insert文を作っていきます。
L formで送られてきたパスワードをハッシュ化して、usrnameと一緒に保存します。
async fn handle_save(form: Form<SignUp>)-> axum::response::Html<String> {
let register: SignUp = form.0;
let password_hash = hashing_password(®ister.password);
println!("{}", &password_hash);
let _ = db_save(®ister.username.to_string(), &password_hash).await.unwrap();
let tera = tera::Tera::new("templates/*").unwrap();
let mut context = tera::Context::new();
context.insert("title", "Index page");
let output = tera.render("index.html", &context);
axum::response::Html(output.unwrap())
}
async fn db_save(name: &String, password: &String) -> Result<(), Box<dyn std::error::Error>> {
let (client, connection) = tokio_postgres::connect("host=localhost user=postgres password=password", NoTls).await?;
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
client.execute(
"INSERT INTO users (username, password) VALUES ($1, $2)",
&[&name, &password],
).await?;
for row in client.query("SELECT id, username, password From users", &[]).await? {
let id: i32 = row.get(0);
let name: String = row.get(1);
let password: String = row.get(2);
println!("{} {} {}", id, name, password);
}
Ok(())
}
Finished `dev` profile [unoptimized + debuginfo] target(s) in 3.95s
Running `target/debug/login`
$2b$08$OxMrzm88ag/iPtKahE3kveHNmmUI5uAuYGuY.lcCybGGqN8cYx/km
1 hpscript $2b$08$bKVELv/My0WaKvoyTltoLe3pOp7VJ4UxtxuA3C5qurnAblnqXbcfi
2 yamada $2b$08$OxMrzm88ag/iPtKahE3kveHNmmUI5uAuYGuY.lcCybGGqN8cYx/km
ここまでは割と簡単なんだけど、問題はここからなんだよな〜